Comments (4)
Thanks for the help. Child-process looks like it runs things in a seperate thread which I'm getting the impression is just a safer, and therefore defacto, way to conduct processing in parallel. Its a shame we can't do this natively in PHP like truely asynchronous languages can (e.g. Javascript).
Yes, the Child Process component lets you spawn a new process you can use to defer blocking CPU intensive operations to. Last time I checked, and I can be wrong here, but JavaScript isn't a multithreaded language. NodeJS does do multi threading by offloading the blocking parts. Found this nice read up about it, for those interested: https://www.digitalocean.com/community/tutorials/how-to-use-multithreading-in-node-js
https://github.com/clue/reactphp-pq looks interesting though its just example code at the moment - doesn't appear to be any actual code that does this job. I suspect @clue would run into the same issue as me: Its easy enough to return simple data types (bool, int, string, float, array) but its difficult to return PHP objects unless there classes have serialize and unserialize magic methods which means you can't share objects easily between threads.
Over the years I've spent a significant amount of time on this. Used both processes and threads for this in PHP. Wrote an overview down in https://blog.wyrihaximus.net/2022/07/my-road-to-fibers-with-reactphp/. And to be honest child processes work, but they are tricky in non-Linux environments, plus they aren't. Wrote my fair share of packages for it, including one that lets you run a givinig callable. https://github.com/WyriHaximus?tab=repositories&q=child-process&type=&language=&sort= However, child processes are heavy, and you need to serialise whatever you're sending between processes. Or you can uses threads, used ext-parallel for that, and then you can toss objects between threads directly. However the bigger the object, the slower it becomes. Set up an entire org to do this with ReactPHP with a ton of different tooling doing a whole bunch of things with it https://github.com/reactphp-parallel/. And I still think it has a place, however, it isn't getting updated since PHP 8.1 got fibers.
TL;DR We provide the tools to make this work. And for all our components, including the filesystem we make sure it works non-blocking, which means that some of the extensions we suggest might use threads under the hood for you just like NodeJS does.
from event-loop.
Hey @fiasco, I think reactphp/child-process is what you're looking for.
Forking can be a possibility here but there are many things you need to be careful about. For example, when forking you create an exact copy of your process, but this doesn't mean that they share the same resources. This could potentially lead to race conditions or even unwanted behavior, all depends on what you're trying to do.
For now the child-process component should fit for your use case, you can also take a look at clue/reactphp-pq.
I hope this helps 👍
from event-loop.
Thanks for the help. Child-process looks like it runs things in a seperate thread which I'm getting the impression is just a safer, and therefore defacto, way to conduct processing in parallel. Its a shame we can't do this natively in PHP like truely asynchronous languages can (e.g. Javascript).
https://github.com/clue/reactphp-pq looks interesting though its just example code at the moment - doesn't appear to be any actual code that does this job. I suspect @clue would run into the same issue as me: Its easy enough to return simple data types (bool, int, string, float, array) but its difficult to return PHP objects unless there classes have serialize and unserialize magic methods which means you can't share objects easily between threads.
from event-loop.
I believe this has been answered, so I'm closing this for now. Please come back with more details if this problem persists and we can always reopen this 👍
from event-loop.
Related Issues (20)
- SIGTSTP not working HOT 4
- Is it possible to export the code of the main loop as a function, so that there is no need to write a callback function. HOT 1
- Manual "tick" execution HOT 11
- Implementations of ::run() use signals->isEmpty() which prevents exiting HOT 3
- PHP8: Uncaught ValueError: No stream arrays wered pased in StreamSelectLoop:: HOT 8
- Loop signals in PHP8 HOT 4
- Infinite recursion bug with timers HOT 6
- How to update the time Interval dynamically? HOT 2
- Periodic timer jumping from one value to another directly. HOT 2
- Experiencing issue with addPeriodicTimer seemingly just stopping randomly HOT 5
- periodic timer with the interval of zero HOT 4
- Rollback the deprecation of the Factory::create method HOT 7
- Question: error missing from register_shutdown_function when using implicit loop vs explicit loop. HOT 2
- [Help] Writing PHPUnit tests - Laravel HOT 3
- Any way to "reset" the loop? HOT 4
- Deprecated: Implicit conversion from float 4995090.007781982 to int loses precision in vendor\react\event-loop\React\EventLoop\StreamSelectLoop.php on line 137 HOT 3
- Watchdog timers HOT 3
- Have a look here HOT 3
- Roadmap to reactphp/event-loop v3 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from event-loop.