Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Sergey Zhuk:
Managing Concurrency: From Promises to Coroutines
Oct 31, 2018 @ 19:56:57

Sergey Zhuk has a post to his site covering concurrency including some of the basic concepts and how promises and coroutines come in to play.

What does concurrency mean? To put it simply, concurrency means the execution of multiple tasks over a period of time. PHP runs in a single thread, which means that at any given moment there is only one bit of PHP code that can be running. That may seem like a limitation, but it brings us a lot of freedom. We don’t have to deal with all this complexity that comes with parallel programming and threaded environment. But at the same time, we have a different set of problems. We have to deal with concurrency. We have to manage and to coordinate it.

He goes on to talk about parallel requests, cross-request needs and the requirement for coordination. He then covers the subject of promises, briefly defining it and giving an example of creating a basic promise in ReactPHP. Following this he talks about the other main topic of the article: using PHP generators along with parallel requests to only create the responses when yielded.

tagged: reactphp promise coroutine concurrency management

Link: https://sergeyzhuk.me/2018/10/26/from-promise-to-coroutines/

Sergey Zhuk:
Amp Promises: From Generators To Coroutines
Feb 15, 2018 @ 15:27:05

In a new post to his site Sergey Zhuk takes a look at generators in PHP and shows how they're integrated into coroutines and promises to help with the chunked asynchronous processing.

Generators become available in PHP since version 5.5.0. The main idea is to provide a simple way to create iterators but without creating a class that implements Iterator interface. A generator function looks like a normal function, except that instead of returning the result once, a generator can yield as many times as it needs to in order to provide the values to be iterated over.

[...] So, let’s wrap up what we know about generators:

  • interruptible/resumable functions
  • use yield rather than (or in addition to) return
  • values/exceptions can be send/thrown into
  • behave like iterator

All these generator features are used in <a href="https://amphp.org/>Amp to implement coroutines.

The post starts out with a pretty detailed look at generators in PHP and the functionality they offer. It covers sending values into the generator, exception handling and the use of the yield statement. It then moves over to describing coroutines, promises and deferred handling. Generators make it easier for them to chunk up the operation one piece at a time rather than requiring all data up front.

tagged: amp promise coroutine generator tutorial integration

Link: http://sergeyzhuk.me/2018/02/15/amp-coroutines/

Christopher Pitt:
Co-operative PHP Multitasking
Mar 30, 2015 @ 17:47:41

Christopher Pitt has posted a new article on Medium.com about when an "array is like an adventure" when in the context of co-operative PHP multitasking. In it he shows how to make code work asynchronously with out the use of extensions, only generators.

Last week I got the opportunity to share recent work with my colleagues, at SilverStripe. I was going to present Async PHP today, but since I covered ReactPHP last week; I decided to talk about something slightly different. So here’s a post about cooperative multitasking.

He starts with some basic arrays and other things that act like them and can be iterated through (Traversable). He talks about implementing custom iterators to act the same way and the use of IteratorAggregate to "cheat" a bit when making them. The he gets into generators, showing how they can be used to iterate similarly. He shows how it's possible to send data to a generator, throwing exceptions inside them and the use of "coroutines" to create asynchronous code. He builds up a queue system with this method and shows how they execute with some simple echo output. He also shows the use of RecoilPHP, another coroutine-based library, to replace the main kernel for a ReactPHP script. He also mentions IcicleIO as another option.

tagged: cooperative multitasking asynchronous code coroutine generator

Link: https://medium.com/@assertchris/co-operative-php-multitasking-ce4ef52858a0

Nikita Popov:
Cooperative multitasking using coroutines (in PHP!)
Dec 24, 2012 @ 15:46:36

Nikita Popov has a new post to his blog about a new feature that will be coming in PHP 5.5 and how to use them, coroutines and generators, in an example application.

Coroutines on the other hand have received relatively little attention. The reason is that coroutines are both a lot more powerful and a lot harder to understand and explain. In this article I'd like to guide you through an implementation of a task scheduler using coroutines, so you can get a feeling for the stuff that they allow you to do. I'll start off with a few introductory sections. If you feel like you already got a good grasp of the basics behind generators and coroutines, then you can jump straight to the "Cooperative multitasking" section.

He starts with a look at generators, a piece of functionality that will allow PHP to, for example, more easily create iterators "on the fly." He then moves on to coroutines, added functions that you have two-way communication with generators instead of just pulling data from them. With the basics out of the way, he gets into the "cooperative multitasking" and a sample socket-based server he implements using some of the concepts.

tagged: generator coroutine multitasking server socket tutorial

Link:


Trending Topics: