php execute a background process

前端 未结 19 1350
萌比男神i
萌比男神i 2020-11-21 07:04

I need to execute a directory copy upon a user action, but the directories are quite large, so I would like to be able to perform such an action without the user being aware

相关标签:
19条回答
  • 2020-11-21 07:44

    I am heavily using fast_cgi_finish_request()

    In combination with a closure and register_shutdown_function()

    $message ='job executed';
    $backgroundJob = function() use ($message) {
         //do some work here
        echo $message;
    }
    

    Then register this closure to be executed before shutdown.

    register_shutdown_function($backgroundJob);
    

    Finally when the response was sent to the client you can close the connection to the client and continue working with the PHP process:

    fast_cgi_finish_request();
    

    The closure will be executed after fast_cgi_finish_request.

    The $message will not be visible at any time. And you can register as much closures as you want, but take care about script execution time. This will only work if PHP is running as a Fast CGI module (was that right?!)

    0 讨论(0)
提交回复
热议问题