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

PHP Roundtable:
044: Asynchronous PHP
Apr 27, 2016 @ 09:23:05

The PHP Roundtable, with host and PHP community member Sammy K Powers, has posted a new episode featuring a discussion about Asynchronous PHP. In this show Sammy is joined by guests Christopher Pitt, Sara Golemon and Aaron Piotrowski.

Async? Isn't that like AJAX in Javascript or something? Most PHP developers encounter asynchronous code for the first time in Javascript, but not many are aware that PHP can do async too. We discuss asynchronous programming in PHP and how we might be able to implement it in our own projects using various libraries. We also take a look at how async features could be added to PHP core to support async natively.

There's plenty of detail in the show notes for this episode covering all of the topics mentioned and links to some other resources you can use to find out more about asynchronous development in PHP. You can listen/watch this latest episode either through the in-page video player or directly on YouTube. If you enjoy the show, be sure to subscribe to their feed and follow them on Twitter for the latest updates when new shows are released.

tagged: phproundtable podcast ep44 asynchronous code discussion

Link: https://www.phproundtable.com/episode/asynchronous-php

SitePoint PHP Blog:
Websockets in Your Synchronous Site
Feb 26, 2016 @ 11:03:53

The SitePoint PHP blog has a new tutorial from Christopher Pitt showing you how to integrate websockets into your application for asynchronous, real-time functionality. His method makes use of a service called Socketize (with a free tier available).

Asynchronous architecture is common in other programming languages, but it’s only just finding its feet in PHP. The trouble is that this new architecture comes with a cost.

I don’t talk about that cost enough. [...] When I recommend frameworks like Icicle, ReactPHP, and AMPHP, the obvious place to start with them is to create something new. [...] It takes a lot of work to integrate new, asynchronous features into existing applications. Often there are good reasons and great benefits, but a rewrite is always a hard-sell. [...] I’m going to show you a Sockets-as-a-Service service, called Socketize.

He walks you through the setup of the code and account to create a simple CRUD (create, read, update, delete) system for a deck of cards. He starts with a simple synchronous API spitting back JSON of the card data. Then he creates the frontend client (simple Javascript) to fetch the data and append the values to the page. Next comes the asynchronous handling - he shows the creation of the Socketize account, setting up a new application (with keys) and making use of the SocketizeJavascript client to create the websocket and hook it all together.

tagged: websockets asynchronous socketize application frontend service thirdparty

Link: http://www.sitepoint.com/websockets-in-your-synchronous-site/

Symfony Finland:
Going Async in Symfony Controllers
Feb 22, 2016 @ 10:50:25

On the Symfony Finland site Jani Tarvainen has posted a tutorial showing you how to create asynchronous controller handling in a Symfony-based application.

Asynchronous programming has become a synonym for high performance in server side web applications in the recent years. This is largely due to the rising popularity of JavaScript and Node.js, in which everything is async by default. [...] So asynchronous programming does not push your computer into overdrive to enable higher performance. What it can do is help the computer to use it's resources more efficiently, by removing time spent waiting.

He then talks about PHP's typical flow model - synchronous and single-threaded. While it does make it simpler to debug/understand it also limits it and can cause higher processing times. Thanks to some other projects, however, asynchronous development with PHP has become more of a reality. He shows how to use one of these projects, Icicle, and its coroutines functionality to make a Symfony controller that handles calls to a sayHello method asynchronously, returning messages in a fraction of the normal processing time.

tagged: asynchronous controller tutorial icicle wait symfony

Link: https://www.symfony.fi/entry/going-async-in-symfony-controllers

SitePoint PHP Blog:
An Introduction into Event Loops in PHP
Sep 10, 2015 @ 10:06:55

The SitePoint PHP blog has posted a tutorial from Christopher Pitt introducing you to using event loops in PHP, a feature that allows for asynchronous processing, executing code while waiting for other code to finish.

PHP developers are always waiting for something. Sometimes we’re waiting for requests to remote services. Sometimes we’re waiting for databases to return rows from a complex query. Wouldn’t it be great if we could do other things during all that waiting?

If you’ve written some JS, you’re probably familiar with callbacks and DOM events. And though we have callbacks in PHP, they don’t work in quite the same way. That’s thanks to a feature called the event loop.

He starts by explaining event loops with an example from a language that naturally supports it - Javascript. He includes another example using the setTimeout function in Javascript to show a simple loop but points out that PHP just doesn't support this same kind of handling (code included). So, how can you simulate the loop like in JS? He links to and includes a examples of two libraries that could be dropped in and used to do the hard work behind the scenes of the looping:

He suggests that PHP developer "get out of the single threaded mindset" and work in ways to handle asynchronous processing into their code to improve performance and flexibility.

tagged: event loop javascript library icicle reactphp asynchronous processing

Link: http://www.sitepoint.com/an-introduction-into-event-loops-in-php/

Cees-Jan Kiewiet:
AWS PHP SDK Asynchronously
Jun 30, 2015 @ 11:31:15

Cees-Jan Kiewiet has a new post today talking about some interesting trickery he was able to do with the AWS (Amazon Web Services) PHP SDK to allow requests to be made asynchronously.

Just got off the AWS SDK for PHP Office Hour hangout and it was great talking with both team members Jeremy and Michael. And one of the things we talked about was async access to the AWS services using the PHP SDK. The goal of this post is to get the AWS PHP SDK client working asynchronously.

He starts with brief instructions on getting the SDK installed (via Composer) along with a library of his own that brings in a few other dependencies. The ReactPHP event loop is what makes the asynchronous connections possible. He includes the code to create the new handler stack and how to use it to make the asynchronous calls. A demo screencast is also included in the post to illustrate the output from a simple set of requests.

tagged: aws amazon sdk asynchronous connection reactphp event loop tutorial

Link: http://blog.wyrihaximus.net/2015/06/aws-php-sdk-asynchronously/

Christopher Pitt:
Co-operative PHP Multitasking
Mar 30, 2015 @ 12: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

Hack Blog:
Async – Cooperative Multitasking for Hack
Dec 08, 2014 @ 11:56:54

On the Hack blog there's a new post talking about async, a feature in Hack that allows for code to "cooperatively multitask". This gives the language a way to keep moving on in the execution without having to wait for things like database queries or remote file fetches to finish.

This is somewhat similar to threading, in that multiple code paths are executed in parallel, however it avoids the lock contention issues common to multithreaded code by only actually executing one section at any given moment. “What’s the use of that?”, I hear you ask. You’re still bound to one CPU, so it should take the same amount of time to execute your code, right? Well, that’s technically true, but script code execution isn’t the only thing causing latency in your application. The biggest piece of it probably comes from waiting for backend databases to respond to queries.

She gives the example of pulling in a remote file (HTTPS, where there's a bit more latency) and how to use async, await, WaitHandle, and Awaitable to work around the timing issue. She shows how to make a method asynchronous and how to join the results of the operation back up with the rest of the script. This includes the use of various "handles" including RescheduleWaitHandle, SleepWaitHandle and the AwaitAllWaitHandle. She shows the integration of a custom cURL handler that makes use of this processing, marked async, to multithread the requests to the remote server(s).

tagged: hack async asynchronous multitasking curl example remote fetch language

Link: http://hhvm.com/blog/7091/async-cooperative-multitasking-for-hack

Michael Dowling:
Guzzle-Ring and Future Responses
Sep 30, 2014 @ 09:36:32

Michael Dowling has a new post to his site today talking about the work that's being done on the upcoming release of the Guzzle HTTP client. In the post he talks about a major change in how it allows for asynchronous requests and the work on Guzzle-Ring to make it happen.

Guzzle 4 has been out for a little over six months. It has proven to be leaps and bounds better than Guzzle 3, and I’ve been very happy with the design so far. However, after the release of Guzzle 4, I’ve received feedback from numerous members of the PHP community that can be boiled down to “Guzzle needs async support.” While Guzzle has always had the ability to send requests concurrently using a pool of requests, there was not a way to send asynchronous requests.

After a couple months of work and borrowing concepts from Clojure, I’ve created Guzzle-Ring, an extremely simple adapter and middleware library for PHP (not just Guzzle) that can power both clients and servers for both synchronous and asynchronous requests.

The Guzzle-Ring reduces the need for the previous complexity of creating multiple adapters, which ended up with the adapters knowing too much about the request itself. He introduces the Guzzle-Ring system that will be included in Guzzle v5, heavily influenced by Clojure. The adapter makes the request as simple as passing in an array and makes use of "futures" to handle the request/response cycle. He also talks some about creating middleware piece that helps integrate it into your application, wrapping functionality inside of another method. He illustrates all of this with code examples and includes others such as fetching of future responses, sending requests concurrently and the Guzzle-Ring server adapters.

tagged: guzzle guzzlering http client asynchronous request futures guzzle5 clojure

Link: http://mtdowling.com/blog/2014/09/28/guzzle-ring/

Qandidate.com Blog:
Asynchronous fault tolerant programming with PHP
Aug 12, 2014 @ 12:17:20

On the Qandidate.com blog there's a new post looking at fault tolerant programming in PHP, a follow up to their previous post introducing the topic and creating a "circuit breaker" to prevent major issues when a problem pops up.

Now we will show you a running demo of the application where the circuit breaker library Phystrix is combined with asynchronous programming. The advantage of this approach is that it allows the querying of multiple backend services asynchronously. A timeout can be set for each of the calls and the circuit breaker will deal with failing services.

A screencast included in the post shows a script in action and how it reacts when the various services it needs fail. They've posted the code for the demo can be found over on GitHub and full instructions to run it are included in the post. Also included is some of the output from the test and a peek inside the logs showing where things were broken and if they were able to recover.

tagged: asynchronous fault tolerant programming demo github screencast

Link: http://labs.qandidate.com/blog/2014/08/11/asynchronous-fault-tolerant-programming-in-php/

Matthias Noback:
Test Symfony2 commands using the Process component and asynchronous assertions
Mar 24, 2014 @ 10:49:13

Matthias Noback has a new post today showing you how to test Symfony2 commands that use the Process component. More specifically, his tests revolve around the ones that use asynchronous assertions in the testing to remove weird behaviors that could be caused by multiple processes running them simultaneously.

Usually you would test a console command using the ConsoleTester class as described in the official Symfony documentation. But we can not use it in this case. We need to isolate the process in order for pcntl_fork() to work correctly, otherwise the PHPUnit test runner itself will be forked too, which will have the effect of running all the following unit tests twice (which can have very strange effects on the test results).

He shows how to use the Process component to start up a new process (the daemon) and check that the PID file exists. He includes an example of a "probe" to determine what processes are running and preventing them from stepping on each other.

tagged: symfony2 command process component asynchronous assertion unittest phpunit

Link: http://php-and-symfony.matthiasnoback.nl/2014/03/test-symfony2-commands-using-the-process-component-and-asynchronous-assertions/