webman icon indicating copy to clipboard operation
webman copied to clipboard

Co existing with another event loop?

Open cayolblake opened this issue 2 years ago • 10 comments

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 🤔

cayolblake avatar Aug 19 '22 19:08 cayolblake

Sorry, I don't have a good solution here.

walkor avatar Aug 20 '22 02:08 walkor

@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?

cayolblake avatar Aug 20 '22 05:08 cayolblake

@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? 🤔

cayolblake avatar Aug 20 '22 05:08 cayolblake

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 avatar Aug 20 '22 07:08 walkor

@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?! 🤔

cayolblake avatar Aug 20 '22 09:08 cayolblake

Theoretically, it is possible.

walkor avatar Aug 20 '22 09:08 walkor

What's required to make a Workerman compliant event loop?

cayolblake avatar Aug 20 '22 13:08 cayolblake

Implement EventInterface see https://github.com/walkor/workerman/blob/master/src/Events/Event.php.

walkor avatar Aug 21 '22 02:08 walkor

@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?

cayolblake avatar Aug 22 '22 12:08 cayolblake

Sorry, I don't have C/FFI event loop related experience and can't help you.

walkor avatar Aug 23 '22 04:08 walkor