Non Blocking File Read in PHP -
i implementing script read data file in php. contains around 30 thousand lines. problem facing takes lot of time server read file , echo results , send page. there way server can send response after given time, 2 seconds.
the result should contain part of file has been processed , in next reply should send part in processed in next 2 seconds.
there couple of approaches this:
1) implement loop in @barmar's answer save current position between requests/responses.
the problem @barmar's approach reads first chunk of file , returns it. it's not clear rest of file. 1 way improve processing via series of requests. example:
client makes request
http://example.com/process?file_position=0
server starts processing @ position 0 responds data first part of file , new file position
client makes request
http://example.com/process?file_position=1000
server reads new position wherever gets in 2 seconds , replies again
...
2) use queue processing system file reading in background , use ajax (or websockets) send periodic updates client.
you can queue system execute long-running jobs in background , implement that. i'm using laravel application , has nice queue system - basically, have worker task running on server waits jobs queued up, executes them. see these docs idea of how works in laravel. may able roll own using combination of packages packagist.
the problem stems php's lack of multithreading support - not possible* spawn new process reading , return data client. long-running tasks either run php's execution time limit or user's browser sits there & spins long time.
* aware there techniques doing this, tend hacks.
Comments
Post a Comment