Tutorial by Examples

PHP 5.5 introduces Generators and the yield keyword, which allows us to write asynchronous code that looks more like synchronous code. The yield expression is responsible for giving control back to the calling code and providing a point of resumption at that place. One can send a value along the yi...
Icicle uses Awaitables and Generators to create Coroutines. require __DIR__ . '/vendor/autoload.php'; use Icicle\Awaitable; use Icicle\Coroutine\Coroutine; use Icicle\Loop; $generator = function (float $time) { try { // Sets $start to the value returned by microtime() after ap...
Amp harnesses Promises [another name for Awaitables] and Generators for coroutine creation. require __DIR__ . '/vendor/autoload.php'; use Amp\Dns; // Try our system defined resolver or googles, whichever is fastest function queryStackOverflow($recordtype) { $requests = [ Dns\qu...
PHP has no support for running code concurrently unless you install extensions such as pthread. This can be sometimes bypassed by using proc_open() and stream_set_blocking() and reading their output asynchronously. If we split code into smaller chunks we can run it as multiple suprocesses. Then usi...
DIO streams are currently not recognized by the Event extension. There is no clean way to obtain the file descriptor encapsulated into the DIO resource. But there is a workaround: open stream for the port with fopen(); make the stream non-blocking with stream_set_blocking(); obtain numeric file...
This is a sample HTTP client class based on Event extension. The class allows to schedule a number of HTTP requests, then run them asynchronously. http-client.php <?php class MyHttpClient { /// @var EventBase protected $base; /// @var array Instances of EventHttpConnection protect...
This is a sample HTTP client based on Ev extension. Ev extension implements a simple yet powerful general purpose event loop. It doesn't provide network-specific watchers, but its I/O watcher can be used for asynchronous processing of sockets. The following code shows how HTTP requests can be sche...

Page 1 of 1