[英]Asynchronous processing using shell_exec is not possible
我正在嘗試在 php 中異步調用 python 腳本。 我正在使用 shell_exec 並且調用成功,但是 php 等待 python 腳本完成處理。 如何讓 php 退出而不等待 python 腳本完成處理?
我將介紹我創建的腳本。
<?php
/* cf. https://ourcodeworld.com/articles/read/207/how-to-execute-a-shell-command-using-php-without-await-for-the-result-asynchronous-in-linux-and-windows-environments */
$time = -hrtime(true);
/* call python script */
$comando = `python run.py`;
shell_exec($comando." > /dev/null &");
/* I would expect the elapsed time to be less than 1 second, but it shows a time that exceeds 5 seconds, the execution time of the python script!!!!!! */
$end = sprintf('%f', ($time + hrtime(true))/1e+9);
echo $end;
?>
import time
def run():
# do anything ...
time.sleep(5)
if __name__ == '__main__':
run()
任何建議將不勝感激。
我找到了原因。
應該寫為字符串的命令被寫在反引號中並在那里執行。
正確的方法是使用單引號。
... snip ...
$comando = `python run.py`;
... snip ...
... snip ...
$comando = 'python run.py';
... snip ...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.