webman
webman copied to clipboard
Co existing with another event loop?
Hi @walkor
I'm using a binary library using ffi and the functionality I'm using that have its own event loop. Once I initialize it and it reaches this part of execution, it blocks, and Workerman fails to execute cause it's not reached.
I know they both execute in the same thread, but thought to ask if there's a possible solution for such issue.
What ideally I would like is having both operate normally without issues.
Any ideas or workarounds 🤔
Sorry, I don't have a good solution here.
@walkor Okay. I'm thinking (out of desperation) to create the external one in onWorkerStart.
Is there a way to create a variable at onWorkerStart and use it in onMessage?
@walkor, so I was able to set variables to be held by the Worker class (not sure if this is bad practice), yet I wasn't able to make it yet.
I am thinking about forking a new Thread for the other component in the onWorkerStart, then communicate to it (I had no idea how to do that yet).
Would that work in theory? 🤔
You can store the data on the worker, for example.
$worker->onWorkerStart = function($worker) {
$worker->someData = 'some data';
};
$worker->onMessage = function($connection, $data) use ($worker) {
echo $worker->someData;
};
The fork process is not recommended. It will lead to unexpected consequences of the event extension, such as the failure of the listening socket.
@walkor okay I understand.
Just a crazy idea. Would it be theoretically possible to share the event loop from within the ffi library and make Workerman utilize it? Similar to when Workerman can optionally utilize Swoole's loop?! 🤔
Theoretically, it is possible.
What's required to make a Workerman compliant event loop?
Implement EventInterface see https://github.com/walkor/workerman/blob/master/src/Events/Event.php
.
@walkor since I'm talking about a C/FFI event loop, any idea or practical example you can provide me to help have an idea how to start?
Sorry, I don't have C/FFI event loop
related experience and can't help you.