So basically, I have a PHP script that I want to execute a Python script from (both are my code), and I want to use fork() within Python to fork it into the background. The problem is that when I do this, the PHP script waits for the child to quit.
I have boiled this issue down to 2 files that illustrate the problem:
bgtest.php:
Code:
<?
$stdout = shell_exec("python bgtest.py; echo shell echo");
print_r($stdout);
?>
bgtest.py:
Code:
#!/usr/bin/env python
import os, time
if os.fork() == 0:
time.sleep(2)
print "child"
else:
print "parent"
Calling the PHP outputs this:
Code:
$ php bgtest.php
parent
shell echo
child
As you can see, the Python fork() works just fine since "shell echo" is output before the child output is.
So I've tried using exec(), shell_exec(), system(), etc from PHP and nothing changes it. I've tried using & and calling disown, but that doesn't work either.
I would really prefer a way that doesn't involve using fork() in PHP since this child is going to be a pretty hefty process so I don't want the overhead of an extra PHP process running (this is the main reason that I am making this fork into the background). I also would like to have the output of the parent process (This is to make sure everything executed okay. Basically it will have exception handling and report back to PHP).
I think the only way this will work is if PHP will completely ignore the fact that there is a child process and continue with its business, but I'm not sure how.
Thanks!