6

How can I run a non blocking system call in PHP?

The system call will call a streaming service run by a second PHP script.. So my page sits and waits on this call.

My two thoughts on a solution:

1: There exists a native method / parameter to execute a system call by non blocking

2: Run system() on a new C++ program that will then fork itself and run the actual php script, on a sep. thread

Is there a native method of executing system calls in a non blocking manner or do I need to hack around this...

I currently have shell_exec('nohup php /path/to/file.php &') but it still holds

4
  • 1
    Have you tried also piping stdout and stderr into files or /dev/null? I would think that nohup would handle that, but worth a try. Commented Sep 4, 2011 at 23:13
  • Crontabs are probably what you're looking for. Crontab PHP Commented Sep 4, 2011 at 23:13
  • If i was looking for Crontabs I'd use them. Im running a spawn of background processes that streams tweets into data files, and have a consumer grab them and distribute them on my site. Not interested in relaying a bunch of pings w/ crontab, need open connection. Commented Sep 4, 2011 at 23:34
  • @Corbin, yes thanks buddy, ended up doing that and it let me move on :) Commented Sep 4, 2011 at 23:35

1 Answer 1

4

From PHP manual:

If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.

An example is provided in a comment on the same page (linux based):

If you want to start a php process that continues to run independently from apache (with a different parent pid) use nohub. Example:

exec('nohup php process.php > process.out 2> process.err < /dev/null &');

Sign up to request clarification or add additional context in comments.

3 Comments

if i dont have a process.out... can i output it into /dev/null then?
That line means: put the output (stdout) of nohup php process.php in a file called process.out, its error stream (stderr) in another file called process.err and take the null stream /dev/null as input (stdin). You can find more details about I/O redirection in bash here: tldp.org/LDP/abs/html/io-redirection.html.
Note that there is a typo in the quoted text - @all, make sure you use nohup, not nohub.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.