A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2009 yearPHP Daemon/worker environment - page 1
User InfoPosts
PHP Daemon/worker environment#1
Problem: I want to implement several php-worker processes who are listening on a MQ-server queue for asynchronous jobs. The problem now is that simply running this processes as daemons on a server doesn(t really give me any level of control over the instances (Load, Status, locked up)...except maybe for dumping ps -aux.
Because of that I(m looking for a runtime environment of some kind that lets me monitor and control the instances, either on system (process) level or on a higher layer (some kind of Java-style appserver)

Any pointers?

posted date: 2009-04-15 07:22:00


Re: PHP Daemon/worker environment#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2009-04-15 07:22:01


Re: PHP Daemon/worker environment#3
Here(s some code that may be useful.<?define((WANT_PROCESSORS(, 5);define((PROCESSOR_EXECUTABLE(, (/path/to/your/processor();set_time_limit(0);$cycles = 0;$run = true;$reload = false;declare(ticks = 30);function signal_handler($signal) { switch($signal) { case SIGTERM : global $run; $run = false; break; case SIGHUP : global $reload; $reload = true; break; } }pcntl_signal(SIGTERM, (signal_handler();pcntl_signal(SIGHUP, (signal_handler();function spawn_processor() { $pid = pcntl_fork(); if($pid) { global $processors; $processors[] = $pid; } else { if(posix_setsid() == -1) die("Forked process could not detach from terminal\n"); fclose(stdin); fclose(stdout); fclose(stderr); pcntl_exec(PROCESSOR_EXECUTABLE); die((Failed to fork ( . PROCESSOR_EXECUTABLE . "\n"); }}function spawn_processors() { global $processors; if($processors) kill_processors(); $processors = array(); for($ix = 0; $ix < WANT_PROCESSORS; $ix++) spawn_processor();}function kill_processors() { global $processors; foreach($processors as $processor) posix_kill($processor, SIGTERM); foreach($processors as $processor) pcntl_waitpid($processor); unset($processors);}function check_processors() { global $processors; $valid = array(); foreach($processors as $processor) { pcntl_waitpid($processor, $status, WNOHANG); if(posix_getsid($processor)) $valid[] = $processor; } $processors = $valid; if(count($processors) > WANT_PROCESSORS) { for($ix = count($processors) - 1; $ix >= WANT_PROCESSORS; $ix--) posix_kill($processors[$ix], SIGTERM); for($ix = count($processors) - 1; $ix >= WANT_PROCESSORS; $ix--) pcntl_waitpid($processors[$ix]); } elseif(count($processors) < WANT_PROCESSORS) { for($ix = count($processors); $ix < WANT_PROCESSORS; $ix++) spawn_processor(); }}spawn_processors();while($run) { $cycles++; if($reload) { $reload = false; kill_processors(); spawn_processors(); } else { check_processors(); } usleep(150000);}kill_processors();pcntl_wait();?>

posted date: 2009-04-15 07:31:00


Re: PHP Daemon/worker environment#4
Do you actually need it to be continuously running?If you only want to spawn new process on request, you can register it as a service in xinetd.

posted date: 2009-04-15 07:33:00


Re: PHP Daemon/worker environment#5
The spawning-aspect isn't a big issue imho because the number of workers is depending on the system performance which is usually constant. More important would be the monitoring aspect of the individual worker status (crashed, whatever). One tool I just discovered for this might be DJBs deamontools

posted date: 2009-04-15 07:43:00


Re: PHP Daemon/worker environment#6
That's one option. For monitoring you could also use flock()-ed PID files. Upon crash all locks are released.

posted date: 2009-04-15 07:51:00


Re: PHP Daemon/worker environment#7
It sounds like you already have a MQ up and running on a *nix system and just want a way to manage workers.A very simple way to do so is to use GNU screen. To start 10 workers you can use:#!/bin/shfor x in `seq 1 10` ; doscreen -dmS worker_$x php /path/to/script.php worker$xendThis will start 10 workers in the background using screens named worker_1,2,3 and so on.You can reattach to the screens by running screen -r worker_ and list the running workers by using screen -list.For more info this guide may be of help:http://www.kuro5hin.org/story/2004/3/9/16838/14935Also try:screen --helpman screenor google.For production servers I would normally recommend using the normal system startup scripts, but I have been running screen commands from the startup scripts for years with no problems.

posted date: 2009-04-15 10:56:00


Re: PHP Daemon/worker environment#8
a pcntl plugin type server daemon for PHPhttp://dev.pedemont.com/sonic/

posted date: 2009-06-04 19:55:00


Re: PHP Daemon/worker environment#9
Where did you get this? Open source project or your own code? Any documentation or explanation of what exactly is going on here?

posted date: 2010-09-02 15:45:00


Re: PHP Daemon/worker environment#10
My own code. I'm not inclined to explain it, no.

posted date: 2010-09-02 22:07:00


Re: PHP Daemon/worker environment#11
Also, github.com/kvz/system_daemon.

posted date: 2011-02-17 07:47:00


Re: PHP Daemon/worker environment#12
@gAMBOOKa: You should write that as a separate answer rather than a comment. :)

posted date: 2011-02-17 07:59:00


select page: « 1 2 »
Copyright ©2008-2017 www.momige.com, all rights reserved.