Re: Command-line PHP script CPU usage goes sky-high, stays there--why?
| From: | Per Jessen | Date: | Tue, 11 Dec 2007 14:34:04 +0000 |
| Subject: | Re: Command-line PHP script CPU usage goes sky-high, stays there--why? | ||
| References: | 1 2 3 4 5 6 7 8 9 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-265683@lists.php.net to get a copy of this message | ||
Jochem Maas wrote:
>> I'd be interested to see how he does the multi-threading in php.
>> Personally I'd always opt for C to write this type of thing, except
>> for perhaps the most simple cases.
>>
>
> any chance of an example from you too?
Sure -
http://jessen.ch/files/distripg_main.c
It can't be compiled, but the pseudo-code goes like this:
initialize
bind() to address(es) to listen to
start a number of threads (=workers)
do until terminated
poll() for new work
if new_work(), accept(), queue it, then wake up the workers.
done
A worker thread:
initialize
do until terminated
wait for work
accept()
process work
done
It wouldn't be too difficult to have threads dynamically started and
stopped depending on the amount of work queued up.
/Per Jessen, Zürich