Starting background processes after a web request

I am interested in starting processes after a web request, or perhaps opening a new process after the completion of the initial thread.

I would prefer not to use cron because of the nature of the tasks I perform and how often they need to be run, waiting for a minute to update is not an option.

I am considering several ways to do this:

1) Calling a page in javascript that starts the process and returns immediately and then starts the tasks after, for example ajax ('/run_jobs.php? Job = 123') .... you get the idea

2) The completion of a new thread after completion of the thread; those. output_page (); new thread (); run_job (123); Output();

Anyone have ideas on this topic or experience with this.

+3
source share
3 answers

Well, it depends on the work you want to run. Another comprehensive, but more scalable and manageable approach is to use Gearman to launch and control tasks. The best part is that you can distribute tasks to other fields so that your web server does not get the full load.

A simple approach is to use exec and nohup, as in

<?php
exec("nohup /usr/bin/PHP .php >/dev/null 2>/dev/null &");
?>

The important part is to separate the output channels from the PHP process. To read the result of a process, you can save it in a database.

+4
source

. . - (, ), ,

Zend Queue PHP.

, , Ajax (HTTP ), , . , . .

+3

, .

Using curl, asynchronously send a request to newprocess.php (or where your new script process is located). You can see the curl_post_async function in How to make an asynchronous GET request in PHP? about how to make such a request.

You might want to do some authentication to make sure that only you can disable newprocess.php ...

+1
source

Source: https://habr.com/ru/post/1725103/


All Articles