I want to write a Multi-threaded PHP Application

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



All,

For years I have wanted to have the ability to create a new Thread in PHP similar to how it is done in the Java language. I understand the complexities this would involve and have mentioned the idea to the PHP internals list once and subsequently had the idea shot down and declared "not likely" until at least PHP 8.0. However, my needs for a thread are very simple and might be able to avoid the complexities of shared memory and synchronization if I could somehow implement even a smaller extension which does what I want. Can you entertain this concept and let me know if you think it is plausible?

I want to write PHP code which looks as follows:

interface Runner {
   public static function run() {}
}
class A_Runner implements Runner {
   ...
   public static function run() {
       echo ("a runner\n");
       sleep(10);
   }
}
class B_Runner implements Runner {
   ...
   public static function run() {
       echo ("b runner\n");
       sleep(10);
   }
}

$A = thread_create(new A_Runner());
$B = thread_create(new B_Runner());

thread_start($A);
thread_start($B);

while (true) {
   ...
   echo ("main\n");
   sleep(10);
}

And the output of this process should display echo statements from all 3 running process simultaneously. For example:

...
a runner
main
b runner
a runner
b runner
main
...

It would be nice if the main script could invoke methods on the $A and $B runner objects in this format:

   $A->my_function(...);
   $B->my_function(...);

But if that wasn't possible because of shared memory issues or synchronization problems, maybe the thread extension could handle marshaling the data on the objects behalf with a function call like:

   thread_call($A,  'my_function',  [param 1, param 2, ... param N]);
   thread_call($B,  'my_function',  [param 1, param 2, ... param N]);

I would want this functionality in order to build stand-alone PHP server applications which continually loop in order to execute system processes like: email notifications, billing, data transfer, transcoding, cron replacements, or even game servers, etc. I currently have a solution in place which does something similar, but it involves IPC and PCNTL and litters my server's 'ps' listing with many child processes. Also, some of the PCNTL child process can become zombies and the whole multi-process model is not as attractive to me as a developer as the multi-threaded one.

Can anyone tell me whether my idea is possible, whether there exists an attempt at a PHP thread-like extension (failed or succeeded), and where I might go to pursue this idea further?

I have already seen the Net_Server PEAR package and am familiar with the 'forking' model used in there. It's not quite what I want. I think Threads are the way to go and am exploring how I can accomplish that.

Dante

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux