V
V
Vadim2015-09-12 01:32:30
PHP
Vadim, 2015-09-12 01:32:30

How to correctly run a long php script?

There was a need to run a php script (namely php and nothing else), which will obviously work for a long time (up to several tens of minutes), while displaying either something like a progress bar or lines with the current execution status. Those. the scheme is as follows: a person clicks on the link and sees some progress until the script is completed.
How is it now customary to do it right? Once for such things I ran it through exec, but suddenly something new has appeared since then?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2015-09-12
@vshemarov

In general, a more or less correct approach:
- the user sends a request to the server
- the server adds a task to the queue
- the daemon handler takes the task and makes it
As for progress tracking, everything is a little more interesting. There are at least 4 implementation options:
- pooling - when periodically we send an ajax request to the server and ask how much has been done there
- long pooling - optimization of the first option, in which the request does not fall off immediately, but falls off either by timeout (say 10 seconds have passed) or the state has changed and we need to notify the user about this. As soon as the connection has fallen off, we process what has come to us or not and repeat the request. Profit - real-time notification, that is, as soon as we have fresh information, we can get it.
- Server-sent events, when the request is given to us in pieces with delimiters. Each piece is given when something on the server has changed. The profit is the same as in the option with long polling, but you do not need to break the connection. But there are a lot of nuances (let’s say it won’t work with Apache) and few people do this.
- web sockets - realtime, full duplex, convenient option, but you need to start a separate daemon.
The simplest option is simple pooling, in your case you don't need realtime, it's enough to ask the server every 10 seconds what's going on. In this case, the queue handler (or child process or whatever) can write the current job status to the cache, and you can get it by ID. you can use redis or memcache as storage, in this regard they are ideal.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question