RE: I want to write a Multi-threaded PHP Application

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

 



I did one (well sort of) once.  

I created a php script which was started by cron every minute.  It read a
mysql table looking for items to process, and on finding one, would stamp
the entry in the table as being processed and it would then proceed with
what it needed to do, which sometimes took about half an hour.  What it did
was a DB2 "select count(*)" on every table in a DB2 mainframe database, and
the task was identified in my mysql table as a list of DB2 databases.  The
script would only start processing if it found one that was not running and
there were less than 5 being currently processed (limiting my DB2
connections to 5 at a time).  The byproduct of all this was that it
prevented the mainframe disk management system from archiving critical
database files which were guaranteed to cause errors when the main corporate
application went live, improving reliability and giving us some numbers to
manage disk space utilization with.

Probably not what you had in mind, but the whole process handled 5
concurrent processes running against 30 databases, once every evening.

Warren Vail

-----Original Message-----
From: D. Dante Lorenso [mailto:dante@xxxxxxxxxxxxxx] 
Sent: Wednesday, April 26, 2006 3:43 PM
To: php-general@xxxxxxxxxxxxx
Subject:  I want to write a Multi-threaded PHP Application

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

-- 
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