运行Python脚本从PHP创建线程,然后在窗口8中将其终止

时间:2017-07-16 00:36:20

标签: php python

我有一个PHP脚本,它运行带有exec的python脚本(" C:/ Python26 / python C:/xampp/htdocs/timeout.py");.它工作得很好,但我不想等待那个python脚本完成。此外,我需要运行剩余的PHP代码(echo" Time Out"),无需等待python reply.Thanks提前。

enter code here
<form action="spg.php" method="POST" enctype="multipart/form-data">
<div align="center"><input type="submit"name="submit" value="INSERT"> &n
    <input type="submit" name="show" value="Show">
</div>
</form>

enter code here
<?php
    $start_time = time();
    $timeVar=true;
    while(true) {
    if ((time() - $start_time) < 300) {
    $read= exec("start C:/Python26/python C:/xampp/htdocs/testing/timeout.py");
  // timeout, function took longer than 300 seconds
}
else
{
  echo "Time out";
  /*sys.exit();*/
}

}

&GT;

enter code here
from multiprocessing import Process
import time

def do_actions():
"""
Function that should timeout after 5 seconds. It simply prints a number and waits 1 second.
:return:
"""
i = 0
while True:
    i += 1
    # print(i)
    time.sleep(1)

if __name__ == '__main__':
# We create a Process
action_process = Process(target=do_actions)

# We start the process and we block for 5 seconds.
action_process.start()
action_process.join(timeout=600)

# We terminate the process.
action_process.terminate()
print("Hey there! I timed out! You can do things after me!")

0 个答案:

没有答案