PHP Asynchronous Streams

Is it possible to somehow make an asynchronous stream in PHP, i.e. so that something runs and requests to the server are processed at the same time and other asynchronous threads can be started?

For example: a user submits material that should be processed for a oooooo long time, and the server processes these materials, as well as other requests at the same time.

Answer 1, authority 100%

You need to do it differently. First, your script will put the given task in a certain place and give the answer that the task is in progress. Further, as the task is accessed, it checks the readiness of its execution and gives an answer in case of success. Another script (hidden from users) takes tasks and starts executing – it can take och. long (for example, it launches cron) and besides, it can be written not in php (which is better). Let php manage the content, and let the C code do the supertasks.

Answer 2

Not in php, asynchrony is ajax technology =) But it’s not scary, even if you don’t know it, now there are a lot of ready-made implementation examples. In particular, ajax forms are just for processing such a plan.

Answer 3

Crutch – the user fills out the form, sends it to the server, the script makes a post-request for itself with some parameter (exec=yes&secrect_code=12345), waits for “OK” from the socket, terminates the connection and writes to the user “Wait “.

Not-quite-a-crutch: exec("php /path/to/script.php");, but there’s something else that needs to be done to not wait for a response.

Answer 4

Out of the box in PHP, this is not possible. However, there is a project called phpDaemonthat allows you to implement asynchronous data processing.

Answer 5

In this scenario, with the use of only a crutch solution is possible. You can’t wait for a real BackgroundWorker. This is basically impossible, since the PHP process doesn’t hang all the time like a .NET or Java virtual machine does, so no threads will live longer than the page is being generated.

Thus, your option is all sorts of tricky manipulations with exec or cron, incron, etc.