Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question. Is there method to change child processes from master process? #1057

Open
kvskkvsk opened this issue Sep 30, 2024 · 1 comment
Open

Comments

@kvskkvsk
Copy link

there is a need to create child processes dynamically. For example, for network connections to other network services as one process per service

I tried that:

use \Workerman\Worker;
use \Workerman\Lib\Timer;

Timer::add(0.001,function(){
	echo 'master worker',PHP_EOL;
	$event = new Worker();
	$event->onWorkerStart = function($worker){
		echo 'master worker2',PHP_EOL;
		\Workerman\Channel\Client::connect('127.0.0.1');
		\Workerman\Channel\Client::on('createChild', function($data){
			echo $data,PHP_EOL;
			Worker::forkOneWorkerForLinux(startWorker());
		});
	};
	$event->run();
},[],false);
function startWorker($timer=false){
	$event = new Worker();
	$event->name = 'Checker-of-services';
	$event->count = 1;
	if($timer){
		$event->onWorkerStart = function($worker){
			echo 'start worker',PHP_EOL;
			\Workerman\Channel\Client::connect('127.0.0.1');
			Timer::add(30,function(){
				echo 'add worker',PHP_EOL;
				echo \Workerman\Channel\Client::publish('createChild', 'dataForNewChildProcess'),PHP_EOL;
			},[],false);
		};
	}
	return $event;
}
startWorker(1);

if(sizeof(Worker::getAllWorkers())){
	Worker::runAll();
}
else echo 'No workers',PHP_EOL;

but this code not run second child process (many errors)
and stopping master not working, maybe because of "\Workerman\Channel\Client::on" in master

PS: my local code of "Worker::forkOneWorkerForLinux" is "public static function"

@kvskkvsk kvskkvsk changed the title Question. Is there method to change child procesess from master process? Question. Is there method to change child processes from master process? Sep 30, 2024
@walkor
Copy link
Owner

walkor commented Sep 30, 2024

Workerman does not support dynamically creating processes, but it allows you to create listeners in the current process.
https://manual.workerman.net/doc/en/worker/listen.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants