←back to thread

116 points doekenorg | 7 comments | | HN request time: 0.317s | source | bottom
1. donatj ◴[] No.44498833[source]
I have read much about Fibers since they were introduced and have never come up with a real use case.

All the examples I find are like the trivial ones here where it just feels like instead of jamming a bunch of code into a single messy function that yields, you'd be better off particularly from a static analysis standpoint just having ordered method calls or chained callables where each step returns the next step as a callable.

I've yet to see a use case where I can't come up with a safer way to do it without Fibers, but I would love if someone could enlighten me because I feel like I am absolutely missing something.

replies(4): >>44499326 #>>44499413 #>>44499430 #>>44502332 #
2. Pesthuf ◴[] No.44499326[source]
I once had a use case where I wrapped a callback based library (Amazon's S3 library I think) call to instead return an iterator. Couldn't do that with only a generator since you can't yield in the callback itself. And it wasnt possible to write a custom iterator class since the library didn't give you back control until it was done.

That was the first and only time they were kinda useful to me.

3. danogentili ◴[] No.44499413[source]
Fibers are incredibly powerful, as they can be used to implement seamless go-like concurrency with async, colorless functions.

They were added to PHP by the maintainers of amphp (https://amphp.org), which is the best library for async PHP out there, providing a clean, object-oriented and async API for files, databases and everything that can make use of async I/O.

replies(1): >>44499581 #
4. hibikir ◴[] No.44499430[source]
I have a service using them a lot. It ultimately relies on a couple dozen downstream dba and endpoints, with some calls requiring strict dependencies, and others running in parallel. So I just draw the dependency chart among the functions, and let the service manage all the parallelism and asynchronous bits with minimal babysitting. If it was all chained, then sure, it's not a significant gain
5. donatj ◴[] No.44499581[source]
A fiber is a colored function. It's a different color than an async function, but it's not blindly swapable for a regular function.
replies(1): >>44508267 #
6. bornfreddy ◴[] No.44502332[source]
I think you're not missing anything (or at least, I'm missing it too - not much advantage over yield). Generators rock though.
7. danogentili ◴[] No.44508267{3}[source]
Async functions based on PHP fibers are explicitly uncolored, they do not require any special keywords to be invoked, and are explicitly swappable with any regular functions.