LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-02-2009, 10:29 PM   #1
Aeiri
Member
 
Registered: Feb 2004
Posts: 307

Rep: Reputation: 30
Python fork() from within PHP


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!
 
Old 10-02-2009, 10:32 PM   #2
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
i thought system() pauses the parent
 
Old 10-02-2009, 11:26 PM   #3
Aeiri
Member
 
Registered: Feb 2004
Posts: 307

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by smeezekitty View Post
i thought system() pauses the parent
Okay, I didn't know that. Thank you for that information. I tried popen() instead:

Code:
<?

$p = popen("python bgtest.py", "r");
$stdout = stream_get_contents($p);
pclose($p);

print_r($stdout);

?>
...and it's still doing the same thing. Does popen() call system() as well?
 
Old 10-02-2009, 11:33 PM   #4
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
i dont know i am not a PHP guru and dont rely 100% on what i say
i just know the basic ins and outs of PHP and a fair amount of C and some C++
 
Old 10-02-2009, 11:33 PM   #5
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
i dont know i am not a PHP guru and dont rely 100% on what i say
i just know the basic ins and outs of PHP and a fair amount of C and some C++
 
Old 10-02-2009, 11:50 PM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
what exactly are you wanting to do?
 
Old 10-03-2009, 10:42 AM   #7
Aeiri
Member
 
Registered: Feb 2004
Posts: 307

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by ghostdog74 View Post
what exactly are you wanting to do?
Well I thought I said it pretty clearly, but I guess I'll word it differently this time.

I am trying to execute a Python process from within PHP, and I want that Python process to fork(), where the parent returns to PHP that it successfully forked, and the child runs in the background on the server and does its tasks as a regular Python application.
 
Old 10-03-2009, 09:39 PM   #8
Aeiri
Member
 
Registered: Feb 2004
Posts: 307

Original Poster
Rep: Reputation: 30
I figured it out, somewhat. It's a bit hacky but it works.

Basically if you call exec() or anything from PHP, it checks to see if stdout/stderr is set to output to the "screen". If you redirect both to somewhere, such as /dev/null, it will immediately go to the next PHP statement. If you need the output of the parent process, the best way I can think of is to do: "&> file & echo !1" at the end. This will return the PID, and then you can monitor the status of the process with "ps" calls. Then you just read from the file you redirected to.

Hope this information helps someone.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Python in Php - issue dianarani Linux - General 0 03-26-2009 11:15 PM
Python or PHP for Web Dev / Mashup jdrietz Programming 3 07-26-2007 04:11 PM
PHP exec() maximum fork? 60s TV Batman Programming 6 03-31-2007 02:50 AM
do you think PHP and python will displace PERL? servnov General 6 10-31-2004 10:23 AM
Perl vs PHP, PYTHON shebang Programming 5 02-14-2004 12:13 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration