Live data from Hacker News

Exploring Coroutines in PHP

doeken.org

51–60 of 68 posts

Re: Exploring Coroutines in PHP

#51

Earlier quoted context omitted.

> like they did with lambdas, that don't capture their environment https://www.php.net/manual/en/functions.arrow.php

Yes this exists, but again introduces a new thing into PHP, rather than using an existing thing. Instead of using existing function syntax and leaving away the name, now one has 2 more things: The "fn" (why can this not be "function" which already exists?) and in combination with "=>". So it is similar to "function ... use ..." which also has the need to introduce a new thing, the "use", instead of simply capturing i…

You might say "PHP is a fractal of bad design"...

Re: Exploring Coroutines in PHP

#52
post #46

Earlier quoted context omitted.

I agree, but I would always pick a different language (like Go) for long running processes. PHP is great for shared-nothing apps though.

Don't worry, we're not comparing languages here. You are free to chose the language and community that fits best to your approach to software development.

Why, thank you for your generosity, you are too kind!

Re: Exploring Coroutines in PHP

#53

Just to clarify my intent with posts like this: I'm not suggesting that PHP is the ideal tool for every use case. The goal is to share a concept that might be unfamiliar to some developers, using PHP as the context. Sometimes learning about a concept in a familiar language helps you recognise where it might be useful elsewhere or apply it in a language that supports it better. Terms like coroutines, concurrency, prom…

Contrived examples are okay if they help trigger a click in the reader's mind, "wow, I could use this to solve problem X in an actual project!"

The difficulty with these examples is that they are very different from the actual tasks that everyday web developers would like to parallelize, such as long-running database queries and API requests. These things often take orders of magnitude longer than any pure-PHP loop that a typical webapp might contain.

An example that fires off an async query and yields the result when it's ready will probably produce the right click in the minds of many more people. (mysqli can do this, but the interface is convoluted and badly in need of a Promise-like wrapper. I'm not sure if PDO/PostgreSQL even supports async queries.)

Re: Exploring Coroutines in PHP

#54
post #53

Just to clarify my intent with posts like this: I'm not suggesting that PHP is the ideal tool for every use case. The goal is to share a concept that might be unfamiliar to some developers, using PHP as the context. Sometimes learning about a concept in a familiar language helps you recognise where it might be useful elsewhere or apply it in a language that supports it better. Terms like coroutines, concurrency, prom…

Contrived examples are okay if they help trigger a click in the reader's mind, "wow, I could use this to solve problem X in an actual project!" The difficulty with these examples is that they are very different from the actual tasks that everyday web developers would like to parallelize, such as long-running database queries and API requests. These things often take orders of magnitude longer than any pure-PHP loop t…

I agree, but I had to draw the line somewhere on this article as it was already getting pretty long. And since I'm tackeling concurrency in the next post, it made more sense to me to start talking more about async there, with examples. Bear with me. Once that post is out, I'm updating this one which will reference the other for async examples. Thank you for your feedback!

Re: Exploring Coroutines in PHP

#55

Earlier quoted context omitted.

It's better to build your app in e.g. PHP, prove its worth, then identify the bottlenecks, THEN determine if you need multi-threading. And if so, determine if PHP would be the best language for it or if you'd be better off going for a different language - one with parallelism / multithreading built into its core architecture. The first logical step after PHP is NodeJS, which has the fast iteration cycles of PHP witho…

> The first logical step after PHP is NodeJS, which has the fast iteration cycles of PHP ... not really, you still have to deal with bundlers in real-world applications.

I use bun and skip builds.

Re: Exploring Coroutines in PHP

#56
post #29

Earlier quoted context omitted.

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.

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.

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.

Re: Exploring Coroutines in PHP

#57

I think neither does PHP have the ecosystem for it, nor does it have the language primitives or standard library for it. Much cleaner languages than PHP already have enough issues getting concurrency things like fibers right. I don't see this ending in any implementation, that doesn't have surprising dysfunctional parts in it. Especially not, when again and again people behind PHP chose the easy way out for adding la…

PHP watched the absolute disaster of python 2 to 3 version upgrade and rightfully decided that it will be as much backwards compatible as possible. PHP 10 with a clean slate is a cute idea that would wreak havoc. Perl, or whatever it is called now, tried this and it was a total disaster.

I personally never perceived Python 2 to 3 as a disaster, and the language has come out better on this end. It would have been a disaster to keep all the cruft of Python 2 forever and keep writing software in it. When Python 3 became stable, I simply never looked back at Python 2 again. Sure, many libraries had to change a bit, to accommodate Python 3. But any sane project would start with Python 3 anyway, and over time Python 2 support would simply fade away, as fewer and fewer people use it. Maybe during some transitional time people had backports of security fixes for Python 2, but in this day and age I wouldn't actually bother with that any longer. Whoever still runs on Python 2 has made a decision to do so, accepted responsibility for not transitioning and has no one but themselves to blame.

Overall they actually did a pretty decent job to transition from Python 2 to 3. Many people didn't have any issues at all, and simply used the new thing instead of the old thing.

Python's language design in itself is debatable of course, but it certainly is better now than with Python 2.

Re: Exploring Coroutines in PHP

#58
post #48
post #30

Earlier quoted context omitted.

PHP doesn’t force keys... You can omit the key and simply write `foreach($items as $value)`

BTW: https://www.php.net/manual/en/iterator.key.php It's literally in the interface.

I’m not sure I follow, what exactly is your complaint? The Iterator interface is described as:

> Interface for external iterators or objects that can be iterated themselves internally

Note “external iterators or objects”. The Iterator interface is not exactly everyday PHP, it’s a specialist utility for making classes iterable so they can be accessed like arrays. Most developers will rarely use it directly, and it’s not being used in the parent comment’s example either.

Iterating over something requires knowing where you are in the sequence, so of course you would need to implement a method to get the current position of the iteration.

Re: Exploring Coroutines in PHP

#59
post #47
post #30

Earlier quoted context omitted.

PHP doesn’t force keys... You can omit the key and simply write `foreach($items as $value)`

What's idiomatic way to get index (0, n-1) with the value? Parent example shows you could not use $key as a generic solution.

You can use the SPL LimitIterator, feed it the generator, and give it the offset and limit.

``` foreach (new LimitIterator($generator, 0, 3) as $value) { echo $value; } ```

https://www.php.net/manual/en/class.limititerator.php

Re: Exploring Coroutines in PHP

#60

Just to clarify my intent with posts like this: I'm not suggesting that PHP is the ideal tool for every use case. The goal is to share a concept that might be unfamiliar to some developers, using PHP as the context. Sometimes learning about a concept in a familiar language helps you recognise where it might be useful elsewhere or apply it in a language that supports it better. Terms like coroutines, concurrency, prom…

I've only read part of the article. So far, it's great.

I haven't used it in ages,but I like PHP...

Post reply on HN