views:

7358

answers:

6

Is there a realistic way of implementing a multi-threaded model in php whether truly or just simulating it. Some time back it was suggested that you can force the operating system to load another instance of the php executable and handle other simultaneous processes.

The problem with this is that when the php code finished executing the php instance remains in memory because there is no way to kill it from within php. so if you are simulating several threads you can imagine whats going to happen. So am still looking for a way multi-threading can be done or simulated effectively from within php. Any ideas?

+4  A: 

PHP has no built-in support for multithreading.

You can use an approach like the one posted by John Lim though:

http://phplens.com/phpeverywhere/?q=node/view/254

Not very robust in my opinion, but it should afford you a bit of multitasking.

cruizer
+1  A: 

Multi-threading is not possible in PHP.

However, it can be simulated as you suggest. I uncovered this article on it. But personally if you require multithreading, I'd say you are using the wrong software stack and maybe you need something a bit more powerful and suited to this kind of thing.

If its just updates or background activity/checks etc, you could run it via a cron task. Without knowing more about the problem you are wishing to solve, its hard to make valid suggestions.

Splash
A: 

While you can't thread, you do have some degree of process control in php. The two function sets that are useful here are:

Process control functions http://www.php.net/manual/en/ref.pcntl.php

POSIX functions http://www.php.net/manual/en/ref.posix.php

You could fork your process with pcntl_fork - returning the PID of the child. Then you can use posix_kill to despose of that PID.

That said, if you kill a parent process a signal should be sent to the child process telling it to die. If php itself isn't recognising this you could register a function to manage it and do a clean exit using pcntl_signal.

J.D. Fitz.Gerald
+1  A: 

You can use exec() to run a command line script (such as command line php), and if you pipe the output to a file then your script won't wait for the command to finish.

I can't quite remember the php CLI syntax, but you'd want something like:

exec('/path/to/php -f '/path/to/file.php' | '/path/to/output.txt');

I think quite a few shared hosting servers have exec() disabled by default for security reasons, but might be worth a try.

adam
It was a couple of years ago when i encountered this problem and eventually did the application using c++ since i also program in that language. Seeing this as an alternate solution, i am going to try it out and see how well it manages.
Steve Obbayi
fyi, the quotes are broken
philfreo
A: 

It is perfectly possible!

With curl setting timeout to 1 and setting the same session_id the for processes you had open, you can communicate with session variables just like my example below with this method you can even close your browser that the process stills exists running on apache

don't forget to verify the correct session id like this http://localhost/test/verifysession.php?sessionid=[the correct id]

startprocess.php

$request = "http://localhost/test/process1.php?sessionid=".$_REQUEST["PHPSESSID"];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $request);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
curl_exec($ch);
curl_close($ch);
echo $_REQUEST["PHPSESSID"];

process1.php

set_time_limit(0);

if ($_REQUEST["sessionid"])
   session_id($_REQUEST["sessionid"]);

function checkclose()
{
   global $_SESSION;
   if ($_SESSION["closesession"])
   {
       unset($_SESSION["closesession"]);
       die();
   }
}

while(!$close)
{
   session_start();
   $_SESSION["test"] = rand();
   checkclose();
   session_write_close();
   sleep(5);
}

verifysession.php

if ($_REQUEST["sessionid"])
    session_id($_REQUEST["sessionid"]);

session_start();
var_dump($_SESSION);

closeprocess.php

if ($_REQUEST["sessionid"])
    session_id($_REQUEST["sessionid"]);

session_start();
$_SESSION["closesession"] = true;
var_dump($_SESSION);
Ricardo
A: 

You could simulate threading. PHP can run background processes via popen (or proc_open). Those processes can be communicated with via stdin and stdout. Of course those processes can themselves be a php program. That is probably as close as you'll get.

Pete