views:

129

answers:

6
A: 

PHP doesn't have this functionality as far as I know

You can emulate the function using a different technique, like this one: Parallel functions in PHP

zaczap
I also thought that PHP does not have such functionality, but I wasn't sure, so I asked here, and it turns out that PCNTL might do the trick.
binaryLV
we learn something new everyday
zaczap
+2  A: 

You can check out PHP Process Control:

http://us.php.net/manual/en/intro.pcntl.php

Note: This is not threading, but the handling of separate processes. There is more overhead attached.

webbiedave
I don't know that I'd say there's more overhead attached in general. On Windows, sure, fork()ing is expensive. But on Linux, it's actually very cheap. The "overhead" you speak of could be seen as needing to deal with IPC (you can't just `return` a value from one process to another, so you do need some code to handle communication)... If your processes are very independent, forking can be the most efficient way to handle them...
ircmaxell
Processes have their own state and memory space. Threads do not. That's the overhead I'm speaking of.
webbiedave
@webbiedave: Quite true, but on Linux, fork() uses copy-on-write memory sharing. So the stack would be copied, but the rest of the process would only be copied when the memory is actually changed. It's actually quite efficient (it can be more efficient than threading if you don't need IPC since it solves the processor affinity problem of threading if you have more than 1 CPU)...
ircmaxell
Right. It will depend on how the OP uses the processes if such overhead will be invoked or not.
webbiedave
+7  A: 

On Unix platforms you can enable the PCNTL functions, and use pcntl_fork to fork the process and run your jobs in child processes.

Something like:

function fast_call_user_func_array($func, $args) {
  if (pcntl_fork() == 0) {
    call_user_func_array($func, $args);
  }
}

Once you call pcntl_fork, two processes will execute your code from the same position. The parent process will get a PID returned from pcntl_fork, while the child process will get 0. (If there's an error the parent process will return -1, which is worth checking for in production code).

Chris Smith
Again, fork() and threads are two completely different things. PHP has no concept of threads. Fork() takes a single running php process and "copies" it on a second process (copying all variables etc). There's no way to return the value from one process to another without some form of IPC. It's VERY different from threading...
ircmaxell
@ircmaxell - right. I guess I was being a bit loose with my terminology. s/thread/process/g :)
Chris Smith
A: 

PHP does not support multi-threading, so there's no other option than taking advantage of the OS or the web server multi processing capabilities. Note that actually you can fetch both the result and output of exec:

string exec ( string $command [, array &$output [, int &$return_var ]] )

nuqqsa
+1  A: 

You can do some threading in PHP if you use the method pcntl_fork.

http://ca.php.net/manual/en/function.pcntl-fork.php

I have never use this myself, but the are some good example of how to use it on php.net.

HoLyVieR
That's not threading. Fork()ing creates a new process as a copy of the current one. You cannot return values or share variables from one process to another (you need to use some form of IPC: sockets, shared memory, fifo files, etc).
ircmaxell
+2  A: 

Wouldn't it solve your problem to fork, keeping the parent process free for other connections & actions? See http://www.php.net/pcntl_fork. If you need an answer back you could possibly listen to a socket in the parent, and write with the child. A simple while(true) loop with a read could possibly do, and probably you already have that basic functionality if you run a permanent TCP server. Another option would be to keep track of your childprocess-ids, keep a accessable store somewhere (file/database/memcached etc), with a pcnt_wait in the main process with a WNOHANG to check which process has exited, and retrieve the data from the store.

Wrikken