获取 PHP 中 python 程序的 运行 状态
Get running status of python program in PHP
你好,我正在和 Raspberry Pi 一起做一个项目。我做了一个 python 程序,里面有一个无限循环。我还制作了一个 PHP 网站,它调用了那个 python 程序,通过这种方式使其在后台 运行:
$call = "sudo python ../python/readver12.py > /dev/null 2>&1 &";
shell_exec($call);
一切似乎都很好,但我不知道如何获取我的 python 程序的状态 运行ning 是否在后台,并通过 [= 使其在我的网站上可用19=] ?
使用 shell_exec() PHP 将等待继续您的脚本,直到您调用的应用程序退出,然后 return 该应用程序的输出将作为字符串。当您的应用程序重新启动时,child 已经完成(或者您已经点击了 PHP time limit)。
听起来您想启动进程并在它处于 运行 时对其进行监视。为此,请查看 proc_open() and proc_get_status().
我想有很多方法可以做到这一点:
尝试从终端获取日志
您可以在 运行 无穷无尽的 python 脚本时在终端上抛出一些日志,并使用 PHP
捕获它
https://docs.python.org/3/howto/logging.html
在文件上写入并用php读取
直截了当,你用 python 写入文件并用 PHP 读取
http://www.pythonforbeginners.com/files/reading-and-writing-files-in-python
API REST 在 PHP 网站上,python 使用 cURL
您可以在 python 脚本中使用 cURL 与 php 中的端点通信并获取所需数据。
cURL:http://pycurl.io/docs/latest/index.html
PHP Api 休息:https://www.codeofaninja.com/2017/02/create-simple-rest-api-in-php.html
希望对你有帮助
你好,我正在和 Raspberry Pi 一起做一个项目。我做了一个 python 程序,里面有一个无限循环。我还制作了一个 PHP 网站,它调用了那个 python 程序,通过这种方式使其在后台 运行:
$call = "sudo python ../python/readver12.py > /dev/null 2>&1 &";
shell_exec($call);
一切似乎都很好,但我不知道如何获取我的 python 程序的状态 运行ning 是否在后台,并通过 [= 使其在我的网站上可用19=] ?
使用 shell_exec() PHP 将等待继续您的脚本,直到您调用的应用程序退出,然后 return 该应用程序的输出将作为字符串。当您的应用程序重新启动时,child 已经完成(或者您已经点击了 PHP time limit)。
听起来您想启动进程并在它处于 运行 时对其进行监视。为此,请查看 proc_open() and proc_get_status().
我想有很多方法可以做到这一点:
尝试从终端获取日志 您可以在 运行 无穷无尽的 python 脚本时在终端上抛出一些日志,并使用 PHP
捕获它https://docs.python.org/3/howto/logging.html
在文件上写入并用php读取
直截了当,你用 python 写入文件并用 PHP 读取 http://www.pythonforbeginners.com/files/reading-and-writing-files-in-python
API REST 在 PHP 网站上,python 使用 cURL 您可以在 python 脚本中使用 cURL 与 php 中的端点通信并获取所需数据。
cURL:http://pycurl.io/docs/latest/index.html
PHP Api 休息:https://www.codeofaninja.com/2017/02/create-simple-rest-api-in-php.html
希望对你有帮助