walkor / webman

Probably the fastest PHP web framework in the world.
https://webman.workerman.net/
MIT License
2.17k stars 214 forks source link

Co existing with another event loop? #331

Open cayolblake opened 2 years ago

cayolblake commented 2 years ago

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 🤔

walkor commented 2 years ago

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

cayolblake commented 2 years ago

@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 commented 2 years ago

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

walkor commented 2 years ago

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.

cayolblake commented 2 years ago

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

walkor commented 2 years ago

Theoretically, it is possible.

cayolblake commented 2 years ago

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

walkor commented 2 years ago

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

cayolblake commented 2 years ago

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

walkor commented 2 years ago

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