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

Timer::add in master process is impossible ? I think it can be #996

Open
webrobot1 opened this issue Jan 6, 2024 · 1 comment
Open

Timer::add in master process is impossible ? I think it can be #996

webrobot1 opened this issue Jan 6, 2024 · 1 comment

Comments

@webrobot1
Copy link

webrobot1 commented Jan 6, 2024

At some situation i need add Timer at master process, but it look like imposible because master have monitorWorkersForLinux method with pcntl_wait function what will be block master process until worker will not shutdown or master will get somу signal. While it was blocking no one Event library can't run timer...but...

Some Event library has own pcntl_wait analogs , like Swoole\Coroutine\System::wait in Swoole (https://wiki.swoole.com/#/coroutine/system?id=wait). It will do same things as pcntl_wait but additional Timer can be work in master process

Is it possible change monitorWorkersForLinux $pid = \pcntl_wait($status, \WUNTRACED);->static::$globalEvent->wait() ? And EventInterface need be abstract with share method wait by default have pcntl_wait (except Swoole) and return array ['pid'=>$pid, 'signal'=>$status] (like a Swoole\Coroutine\System::wait return array's format) or stay like Interface but need have abstract method wait():array (but in this realisation each Event need wait method with pcntl_wait except Swoole)

Because not it impossible change monitorWorkersForLinux by extends class , because it have private property , additionaly this method have a lot of code, but only need change one line of code

@walkor

@walkor
Copy link
Owner

walkor commented Jan 12, 2024

Hi webrobot1

Workerman support timer in the master process, and the code is similar to this

$worker = new Worker('http://0.0.0.0:1234');
// The timer will only run in master process
Timer::add(1, function() {
    echo "hello";
});
Worker::runAll();

take care
The timer of the main process only supports second level.
It is not recommended to initialize database or Redis resource class connections in the main process, as these connection resources may be inherited by child processes, leading to unforeseen problems

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