并行执行 PHP
Parallel execution of PHP
我有一个 php 脚本文件 test1.php
<?php
echo "hello\n"
?>
当我尝试使用以下代码通过命令行 运行 php
时,只有一个 php 进程 运行 和其他 php 进程停止。
我们可以看到只打印了一个hello
。
为什么其他进程停止了?
[root@home usr]# php -f test1.php & php -f test1.php
[30] 20817
hello
[root@home usr]#
[30]+ Stopped php -f test1.php
PS:
PHP 5.4.16 (cli) (built: Jun 23 2015 21:17:27)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
脚本并行执行。
试试这个测试脚本的修改版本:
<?php
// Read first command line argument
$delay = (int) $argv[1];
// Wait for $delay seconds
sleep($delay);
// Output some message
echo "Result after $delay seconds\n";
如果您通过 php hello.php 3 & php hello.php 1
调用此脚本,您将看到第一个调用将 运行 持续 3 秒并打印一些输出,而第二个调用将仅 运行 持续 1 秒并在第一次调用发送输出之前打印一些输出:
root@httpserver:/tmp# php hello.php 3 & php hello.php 1
[1] 15991
Result after 1 seconds
root@httpserver:/tmp# Result after 3 seconds
如果您正在寻找并行脚本执行的其他方式,您应该看看这篇文章 https://d-mueller.de/blog/parallel-processing-in-php/
我有一个 php 脚本文件 test1.php
<?php
echo "hello\n"
?>
当我尝试使用以下代码通过命令行 运行 php
时,只有一个 php 进程 运行 和其他 php 进程停止。
我们可以看到只打印了一个hello
。
为什么其他进程停止了?
[root@home usr]# php -f test1.php & php -f test1.php
[30] 20817
hello
[root@home usr]#
[30]+ Stopped php -f test1.php
PS:
PHP 5.4.16 (cli) (built: Jun 23 2015 21:17:27)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
脚本并行执行。
试试这个测试脚本的修改版本:
<?php
// Read first command line argument
$delay = (int) $argv[1];
// Wait for $delay seconds
sleep($delay);
// Output some message
echo "Result after $delay seconds\n";
如果您通过 php hello.php 3 & php hello.php 1
调用此脚本,您将看到第一个调用将 运行 持续 3 秒并打印一些输出,而第二个调用将仅 运行 持续 1 秒并在第一次调用发送输出之前打印一些输出:
root@httpserver:/tmp# php hello.php 3 & php hello.php 1
[1] 15991
Result after 1 seconds
root@httpserver:/tmp# Result after 3 seconds
如果您正在寻找并行脚本执行的其他方式,您应该看看这篇文章 https://d-mueller.de/blog/parallel-processing-in-php/