swoole / library

📚 Swoole Library
https://wiki.swoole.com/#/library
Apache License 2.0
242 stars 57 forks source link

Feature request: Proroutines (?) #84

Open leocavalcante opened 3 years ago

leocavalcante commented 3 years ago

Hi 👋

I was zapping my social feed when a suddenly found this: https://github.com/clue/reactphp-pq Looks interesting because of the easiness it provides (Laravel surfs a lot on this), I mean... I know it is "just" multi-processing and Swoole already does a really good job, but!!

What about and API that treats a new process like if its a new "Coroutine"?

A Proroutine (or a better name) is like a Coroutine, but it is actually a new process and all of this could be just a sugar, in the library, around Pool and Manager? Then whatever is blocking, like unimplemented I/O protocols or CPU-bound tasks, you can delegate to a Proroutine instead of a Coroutine. With the proper disclaimer that a Proroutine is not arbitrary created like a green-thread/coroutine.

Procroutines looks cool as well.

fakharksa commented 2 years ago

@leocavalcante Creating heavy OS-Processes, one merely for each Coroutine (where Coroutine is just an I/O-bound Asynchronous Task) is against the core philosophy / primitives of "Asynchronous Programming Paradigm", hence not highly Scalable, not cost-effective, not resource-efficient and low in Performance at scale.

The philosophy behind Coroutines is that "Multiple Coroutines (tasks) should be able to run within same OS-process, through an Event-loop in order to mimic as if light-weight (virtual-/green-) threads; also called as Fiber in modern Asynchronous Programming Paradigm. This is the reason why Coroutines are more easier-to- use/maintain, cost-effective and resource-efficient compared to multi-threading and multi-processing.

You can have multiple OS-Processes but each should be able to run multiple coroutines (Asynchronous/Concurrent/Non-blocking Tasks).

Note:

  1. Swoole allows you to delegate CPU-bound (long-running) Tasks to be delegated to separate OS-Processes called as Task Workers. Task Workers run Asynchronously by default (but can also be asked to execute Synchronously, if needed.)

  2. Apart from Task Workers, Swoole additionally allows to create multiple child Processes, but you should not use them as replacement to Coroutines; that is not a good idea as OS-Process is the heaviest thing for memory in Operating Systems, so it should be used efficiently with careful planning.