5 ms·
Question : modern PHP is great for websites, but what are the solutions when you need to make some async work ? For example if I need to make few third party AP
by EZ-E 8y ago
Question : modern PHP is great for websites, but what are the solutions when you need to make some async work ? For example if I need to make few third party API calls in parallel ? With Laravel I resorted to using queues which feels like more complicated than it had to be (each worker takes up quite a bit of memory). Eventually I switched to SQS + Lambda + node.js.
Is there any mature way to do async work in PHP ? In Node.js for example, it's quite trivial and powerful with promises and async/await
- no_gravity 8y agoOne easy way to do multiple http requests in parallel are the curl_muli functions: http://php.net/manual/en/function.curl-multi-init.php http://php.net/manual/en/function.curl-multi-init.php
- EZ-E 8y agoThanks, that's one solution if making multiple calls is needed. That was mostly an example though, for example if I wanted to run 3 functions in parallel, I don't find any "easy" and elegant way. Some libraries exist like amp, but it seems a bit heavy handed as the language itself doesn't have great tools for it https://github.com/amphp/amp https://github.com/amphp/amp
- no_gravity 8y agoWhy do you want to run 3 functions in parallel?
- EZ-E 8y agoIt's an example. For example if each of these functions takes about 0.2 seconds to finish, it might make sense to run them in parallel
- moojd 8y agoNeither amphp or reactphp will let you start running blocking functions in parallel out of the box. They basically just provide syntactic sugar for making single-threaded, cooperative multitasking easier by wrapping php's native ability to read/write to sockets asynchronously (or by using pecl extensions.) So you can respond to websocket messages or http requests asynchronously but you're not going to be able to implement anything like a parallel merge sort were you offload work to other threads. Things like general purpose file IO are not easy at the moment either. Amphp might seem a heavy handed because they've taken the monorepo approach. ReactPHP on the other hand has broken everything up into individual components. For example, you can get started with ReactPHP by just pulling in the event loop https://github.com/reactphp/event-loop https://github.com/reactphp/event-loop. All the heavy lifting for the native loop is <200 lines https://github.com/reactphp/event-loop/blob/master/src/StreamSelectLoop.php https://github.com/reactphp/event-loop/blob/master/src/Strea... and the API is tiny as well https://github.com/reactphp/event-loop/blob/master/src/LoopInterface.php https://github.com/reactphp/event-loop/blob/master/src/LoopI...
- jsjohnst 8y ago> For example if I need to make few third party API calls in parallel ? There’s curl-multi[0] you can use for that purpose. Not as nice as promises/async/await, but it gets the job done for your example. [0] https://secure.php.net/manual/en/function.curl-multi-exec.php https://secure.php.net/manual/en/function.curl-multi-exec.ph...
- fimbulvetr 8y agoYou might want to check out gearmand and/or any of it's spiritual successors.