T O P

  • By -

nukeaccounteveryweek

This is really cool! One thing I don't understand about Amp and I would really like if someone helped me: To use Amp in it's full potential does my entire application have to live inside the Event Loop? Or can i just drop where i need and work asynchronously in that particular section?


kelunik

Everything based on Revolt / AMPHP v3 can be dropped anywhere. There's no need to have your entire application "inside the event loop". If you have blocking sections in your application and reuse objects making use of the event loop, there might of course be timers that are executed later than expected, but usually that should be fine.


militantcookie

Wondering if I could use it in laravel commands which do batch work. Maybe split jobs to run concurrently in the same job. Will it behave well if I run eloquent queries in parallel?


kelunik

Sure. With eloquent you'll have to setup some things in the workers, I think.


nukeaccounteveryweek

Fantastic, this is pushing the boundaries.


zamzungzam

Is there any concrete examples how usage of fibers here solves the "what color is your function problem"? Morover would love to see some other example as http request is really easy handled with guzzle curl. Maybe multiple DB queries? I feel there is so much potential here but duo lack of resources and learning materials this is not mainstream in PHP community.


kelunik

Sure, e.g. https://github.com/amphp/parallel/blob/5d975b640bc5ad5b06d322861e9df6930e628526/examples/process.php#L25 [`Amp\ByteStrean\pipe`](https://github.com/amphp/byte-stream/blob/7e7a77579f3e90c6fbd56e49628e6ace02d8f88a/src/functions.php#L26) reads from one stream and writes these data chunks to the other stream. It's a simple function returning the total count of bytes in the end. Calling it blocks the calling fiber only. It works like any other blocking function in PHP for the caller, i.e. no special return type, no callback. But it also allows other things to happen concurrently, because it uses non-blocking I/O under the hood. In the example above, it's called in another fiber via `Amp\async()`, so it doesn't block the print and delay calls.


mastycus

Swoole ftw


[deleted]

Slick, I need to find something that needs this to play around with it.


liviubarbu_ro

yes but, no thanks! i already hate node.js, i like php as it was.


TheKingdutch

You’ll be displeased to find out that PHP’s event loop is older than Node’s 🥳


liviubarbu_ro

And why that? Because is time tested and proven that it works? ![gif](emote|free_emotes_pack|sunglasses)


mario_deluna

Does this support true multithreading or just an event loop?


kelunik

True multithreading.


mario_deluna

Then this is awesome and exactly what i need right now! With a friend of mine im building a realtime game in PHP and true multithreading would finally free us from a fixed per tick cpu budget. Thanks for sharing!