- PHP Reactive Programming
- Martin Sikora
- 174字
- 2021-07-09 19:06:18
The proc_open() and non-blocking fread()
Our goal is to have the means to start various subprocesses asynchronously. In this example, we'll use a simple PHP script that'll just sleep for a couple of seconds and represent our asynchronous task:
// sleep.php $name = $argv[1]; $time = intval($argv[2]); $elapsed = 0; while ($elapsed < $time) { sleep(1); $elapsed++; printf("$name: $elapsed\n"); }
This script takes two arguments. The first one is an identifier of our choice that we'll use to distinguish between multiple processes. The second one is the number of seconds this script will run while printing its name and the elapsed time every second. For example, we can run:
$ sleep.php proc1 3 proc1: 1 proc1: 2 proc1: 3
Now, we'll write another PHP script that uses proc_open()
to spawn a subprocess. Also, as we said, we need the script to be non-blocking. This means that we need to be able to read output from the subprocess as it is printed using printf()
above, while being able to spawn more subprocess, if needed:
// proc_01.php $proc = proc_open('php sleep.php proc1 3', [ 0 => ['pipe', 'r'], // stdin 1 => ['pipe', 'w'], // stdout 2 => ['file', '/dev/null', 'a'] // stderr ], $pipes); stream_set_blocking($pipes[1], 0); while (proc_get_status($proc)['running']) { usleep(100 * 1000); $str = fread($pipes[1], 1024); if ($str) { printf($str); } else { printf("tickn"); } } fclose($pipes[1]); proc_close($proc);
We spawn a subprocess php sleep.php proc1 3
and then go into a loop. With a 100ms delay, we check whether there's any new output from the subprocess using fread()
. If there is, we print it; otherwise, just write the word "tick". The loop will end when the subprocess terminates (that's the condition with the proc_get_status()
function).
The most important thing in this example is calling the stream_set_blocking()
function, which makes operations with this stream non-blocking.
- 編程的修煉
- Photoshop智能手機APP UI設計之道
- 深入淺出Prometheus:原理、應用、源碼與拓展詳解
- 高級C/C++編譯技術(典藏版)
- Hands-On Enterprise Automation with Python.
- Responsive Web Design by Example
- Linux命令行與shell腳本編程大全(第4版)
- Hands-On Automation Testing with Java for Beginners
- 從0到1:Python數據分析
- 深入分布式緩存:從原理到實踐
- INSTANT Sinatra Starter
- Deep Learning with R Cookbook
- Practical Predictive Analytics
- C#面向對象程序設計(第2版)
- iOS開發項目化入門教程