How to execute a command via proc_open and not wait for the execution to finish?

Asked

Viewed 353 times

1

I own a Symfony2 project that generates a PDF from an HTML with Bundle KnpSnappyBundle, that is dependent on Bundle KnpSnappy and which, in turn, makes the abstraction with the executable wkhtmltopdf.

This binary is executed with the appropriate parameters through the function proc_open PHP as per line below:

$process = proc_open($this->command, $descriptorspec, $pipes, null, $this->env);

In the tests I did, I noticed that the process gets stuck with the Apache process. So if the server is overloaded, the Apache response may take a long time, which is not an option.

Changing the function as well or the Bundle functionality is not an option either. The only things I can do is modify the function parameters proc_open so that the process doesn’t get stuck with Apache - if this option exists.

Has anyone done anything like this or knows any workaround?

  • Look, I’ve been through this problem too, a workaround I did at the time was to use a proc_open on an SSH and execute the command I wanted in SSH assigning the process to the background, then disconnecting. But it’s one of those tricks. But it works, because I was lifting services and I couldn’t leave my application hanging on them.

  • @Gabrielgartz the solution I found would be to put a & at the end of the command, but as generation process is quite encapsulated, I have no way to do this.

  • It is the problem of using & at the end, which you run in the background but the process is tied to whoever fired it, that is, if you kill the PHP process (which tends to end at the end of the script) it will stop the hanging process and if it is not finished, it will be canceled.

1 answer

1

Hello, I had a similar problem and I solved using a system of Queue and putting the script to run asynchronously.

Take a look at the Gearman and see how simple it is to use: Gearmanclient::doBackground

  • Gearman actually solves some cases but in my specific case I need it to work according to the parameters I pass to the proc_open.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.