| Start |
|---|
| Advantages of Generators PHP 5.5 introduces Generators and the yield keyword, which allows us to write asynchronous code that looks more like synchronous code. The This mechanism can be used by a coroutine implementation to wait for Awaitables yielded by the Generator (by registering itself as a callback for resolution) and continue execution of the Generator as soon as the Awaitable is resolved. |
| Using Icicle event loop Icicle (opens new window) uses Awaitables and Generators to create Coroutines. |
| Spawning non-blocking processes with proc_open() PHP has no support for running code concurrently unless you install extensions such as If we split code into smaller chunks we can run it as multiple suprocesses. Then using As an example we can have a small subprocess that just runs a loop and in each iteration sleeps randomly for 100 - 1000ms (note, the delay is always the same for one subprocess). Then the main process will spawn subprocesses and read their output. We can split it into smaller blocks:
The output then contains mixture from all three subprocesses as they we're read by fread() (opens new window) (note, that in this case |
| Using Amp event loop Amp (opens new window) harnesses Promises [another name for Awaitables] and Generators for coroutine creation. |
| Reading serial port with Event and DIO DIO (opens new window) streams are currently not recognized by the Event (opens new window) extension. There is no clean way to obtain the file descriptor encapsulated into the DIO resource. But there is a workaround:
dio.php TestingRun the following command in terminal A: The output may be different. Use the PTYs from the first couple of rows ( In terminal B run the above-mentioned script. You may need root privileges: In terminal C send a string to the first PTY: Output |
| HTTP Client Based on Event Extension This is a sample HTTP client class based on Event (opens new window) extension. The class allows to schedule a number of HTTP requests, then run them asynchronously. http-client.phptest.phpThis is a sample script on the server side. UsageSample Output (Trimmed.) Note, the code is designed for long-term processing in the CLI SAPI (opens new window). |
| HTTP Client Based on Ev Extension This is a sample HTTP client based on Ev (opens new window) extension. Ev extension implements a simple yet powerful general purpose event loop. It doesn't provide network-specific watchers, but its I/O watcher (opens new window) can be used for asynchronous processing of sockets (opens new window). The following code shows how HTTP requests can be scheduled for parallel processing. http-client.phpTestingSuppose Then the output of (trimmed) Note, in PHP 5 the sockets extension may log warnings for |
Read Article
https://develop.horje.com/learn/1434/reference