Home > Backend Development > PHP Tutorial > php中popen之后一直等待脚本执行完毕才向下执行,如何解决?

php中popen之后一直等待脚本执行完毕才向下执行,如何解决?

WBOY
Release: 2016-06-06 20:07:36
Original
1883 people have browsed it

我在PHP中,使用了pclose(popen())之后,一直等待popen运行的脚本运行完成才继续向下执行。

<code>//test.php
pclose(popen('php '.__DIR__.'/index.php','r'));
$fp = fopen('product.txt', 'cb');
if(flock($fp, LOCK_EX | LOCK_NB)){
    echo "ok<br>";
}else{
    echo "no<br>";
}
fclose($fp);

</code>
Copy after login
Copy after login

<code>//index.php
sleep(10);
set_time_limit(0);
$fp = fopen('product.txt', 'cb');
flock($fp, LOCK_EX | LOCK_NB);
for($i = 10 ; $i ;$i--){
    fwrite($fp,$i."\n");
    sleep(6);
}
fclose($fp);</code>
Copy after login
Copy after login

在test.php中第2行代码一直等到index.php执行完毕才开始运行,请问这是为什么

回复内容:

我在PHP中,使用了pclose(popen())之后,一直等待popen运行的脚本运行完成才继续向下执行。

<code>//test.php
pclose(popen('php '.__DIR__.'/index.php','r'));
$fp = fopen('product.txt', 'cb');
if(flock($fp, LOCK_EX | LOCK_NB)){
    echo "ok<br>";
}else{
    echo "no<br>";
}
fclose($fp);

</code>
Copy after login
Copy after login

<code>//index.php
sleep(10);
set_time_limit(0);
$fp = fopen('product.txt', 'cb');
flock($fp, LOCK_EX | LOCK_NB);
for($i = 10 ; $i ;$i--){
    fwrite($fp,$i."\n");
    sleep(6);
}
fclose($fp);</code>
Copy after login
Copy after login

在test.php中第2行代码一直等到index.php执行完毕才开始运行,请问这是为什么

pclose(popen('nohup ' . $cmd . ' & 2>&1', 'r'));

pclose(popen('php /path/to/task.php &', 'r')); 其中&表示放入后台执行脚本task.php.
另外注意,if(flock($fp,LOCK_EX)) 会阻塞到获取排它锁.

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template