phpgt / daemon
此包最新版本(v1.1.4)没有可用的许可信息。
跨平台兼容的流式脚本执行。
v1.1.4
2024-05-16 08:42 UTC
Requires
- php: >=8.1
- ext-pcntl: *
Requires (Dev)
- phpmd/phpmd: ^2.13
- phpstan/phpstan: ^v1.10
- phpunit/phpunit: ^10.5
- squizlabs/php_codesniffer: ^3.7
This package is auto-updated.
Last update: 2024-09-12 13:29:24 UTC
README
使用面向对象的过程池异步执行后台进程。
示例用法
<?php use Gt\Daemon\Process; use Gt\Daemon\Pool; // Create three long-running processes: $pingProcess = new Process("ping google.com"); $infiniteProcess = new Process("while true; do echo 'background...'; sleep 3; done"); $dateProcess = new Process("while true; do echo $(date -d now); sleep 2; done"); // Add all three processes to a pool: $pool = new Pool(); $pool->add("Ping", $pingProcess); $pool->add("Loop", $infiniteProcess); $pool->add("Date", $dateProcess); // Start the execution of all processes: $pool->exec(); // While processes are running, write their output to the terminal: do { echo $pool->read(); // Sleep to avoid hogging the CPU. sleep(1); } while($pool->numRunning() > 0);
输出类似以下内容
[Ping] PING google.com (172.217.169.78) 56(84) bytes of data.
[Ping] 64 bytes from lhr48s09-in-f14.1e100.net (172.217.169.78): icmp_seq=1 ttl=52 time=8.78 ms
[Loop] background...
[Date] Mon 19 Aug 09:58:54 BST 2019
[Ping] 64 bytes from lhr48s09-in-f14.1e100.net (172.217.169.78): icmp_seq=2 ttl=52 time=8.75 ms
[Ping] 64 bytes from lhr48s09-in-f14.1e100.net (172.217.169.78): icmp_seq=3 ttl=52 time=8.75 ms
[Date] Mon 19 Aug 09:58:56 BST 2019
[Ping] 64 bytes from lhr48s09-in-f14.1e100.net (172.217.169.78): icmp_seq=4 ttl=52 time=8.75 ms
[Loop] background...
[Ping] 64 bytes from lhr48s09-in-f14.1e100.net (172.217.169.78): icmp_seq=5 ttl=52 time=8.80 ms
[Date] Mon 19 Aug 09:58:58 BST 2019
注意日期处理过程仅设置为循环三次,完成后其他两个无限过程继续运行。