Netroby wrote on 07/12/2015 07:05:
As the RFC said.
https://wiki.php.net/rfc/generator-delegation
The defining feature of Generator functions is their support for
suspending execution for later resumption. This capability gives
applications a mechanism to implement asynchronous and concurrent
architectures even in a traditionally single-threaded language like
PHP. With simple userland task scheduling systems interleaved
generators become lightweight threads of execution for concurrent
processing tasks.
I wrote a small test. to see if it really concurrent processing any tasks.
the result looks bad. Still single blocking thread.
You might be interested in this recent thread:
http://marc.info/?t=144342922700001&r=1&w=2 Particularly the discussion
of parallel vs asynchronous execution here:
http://marc.info/?l=php-internals&m=144354107223549&w=2
Generators / co-routines can be useful when working with asynchronous
APIs - that is, functions which start a background task and notify when
it is completed - but they do not magically make sequential code
parallelizable. Threads - true parallel execution - are much more
complex to implement, because of the need to access shared resources in
a safe way.
So there is nothing surprising in your example not running in parallel:
"yield" does not start a new thread, and "sleep" is not an asynchronous
API. If instead of sleep you called an asynchronous function, such as a
MySQL call set to asynchronous mode, you would indeed see your
co-routines running concurrently, as each waited for its respective
query result to come back from the database.
Hope that helps point you in the right direction to better understand
what's going on.
Regards,
--
Rowan Collins
[IMSoP]
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php