Run an asynchronous PHP task using Symfony Process
Main script must be waiting when async process will be completed. Try this code:
$process = new Process('php subscript.php');$process->start();do { $process->checkTimeout();} while ($process->isRunning() && (sleep(1) !== false));if (!$process->isSuccessful()) { throw new \Exception($process->getErrorOutput());}
If php supports fpm for Windows, you can listen to kernel.terminate
event to provide all expensive tasks after response has been sent.
Service:
app.some_listener: class: SomeBundle\EventListener\SomeListener tags: - { name: kernel.event_listener, event: kernel.terminate, method: onKernelTerminate }
Listener:
<?phpnamespace SomeBundle\EventListener;use Symfony\Component\HttpKernel\Event\PostResponseEvent;class SomeListener{ public function onKernelTerminate(PostResponseEvent $event) { // provide time consuming tasks here }}
Not the best solution, but:
$process = new Process('nohup php subscript.php &');$process->start();