PHP 和 Python 之间的通信
我正在尝试为一些 python 脚本构建一个网络界面。问题是我必须使用 PHP(而不是 CGI),并且我执行的一些脚本需要相当长的时间才能完成:5-10 分钟。 PHP 是否可以与脚本进行通信并显示某种进度状态?这应该允许用户在任务运行时使用网页,并同时显示一些状态或在任务完成时仅显示一条消息。
当前使用 exec() 并在完成后处理输出。服务器在 Windows 计算机上运行,因此 pcntl_fork 将不起作用。
稍后编辑: 使用另一个 php 脚本使用 ajax 来提供主页信息似乎不起作用,因为服务器杀死了它(它达到了最大执行时间,除非有必要,否则我真的不想增加它)
我正在考虑基于套接字的通信但我不明白这对我的情况有什么用(也许有一些提示?
谢谢
I'm trying to build a web interface for some python scripts. The thing is I have to use PHP (and not CGI) and some of the scripts I execute take quite some time to finish: 5-10 minutes. Is it possible for PHP to communicate with the scripts and display some sort of progress status? This should allow the user to use the webpage as the task runs and display some status in the meantime or just a message when it's done.
Currently using exec() and on completion I process the output. The server is running on a Windows machine, so pcntl_fork will not work.
LATER EDIT:
Using another php script to feed the main page information using ajax doesn't seem to work because the server kills it (it reaches max execution time, and I don't really want to increase this unless necessary)
I was thinking about socket based communication but I don't see how is this useful in my case (some hints, maybe?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要进程间通信。首先想到的是套接字;您需要在 PHP 中设置一个套接字来监听连接(在同一台机器上),并在 Python 中设置一个套接字连接到监听套接字发送它的状态。
查看 Python 文档中的此套接字编程概述和Python
socket
模块的文档(尤其是最后的示例)。我确信 PHP 也有类似的资源。一旦您对想要构建的内容有了更具体的了解并需要帮助,请随时在 StackOverflow 上提出新问题(如果尚未得到解答)。
You want inter-process communication. Sockets are the first thing that comes to mind; you'd need to set up a socket to listen for a connection (on the same machine) in PHP and set up a socket to connect to the listening socket in Python and send it its status.
Have a look at this socket programming overview from the Python documentation and the Python
socket
module's documentation (especially the examples at the end). I'm sure PHP has similar resources.Once you've got an more specific idea of what you want to build and need help, feel free to ask a new question on StackOverflow (if it isn't already answered).
我认为你必须使用元刷新,也许让 python 将状态写入文件,然后让 php 从中读取。
您也可以使用 AJAX 使其更加动态。
另外,可能不应该使用 exec()...这会打开一个充满漏洞的世界。
I think you would have to use a meta refresh and maybe have the python write the status to a file and then have the php read from it.
You could use AJAX as well to make it more dynamic.
Also, probably shouldn't use exec()...that opens up a world of vulnerabilities.
您可以使用像 Gearman 这样的队列服务,其中包含 PHP 客户端和 Python 工作线程,反之亦然。
有人在这里创建了一个示例设置。
https://github.com/dbaltas/gearman-python-worker
You could use a queuing service like Gearman, with a client in PHP and a worker in Python or vice versa.
Someone has created an example setup here.
https://github.com/dbaltas/gearman-python-worker
不幸的是,我的朋友,我确实相信您需要按照您的要求使用套接字。 :( 我几乎没有使用它们的经验,但是 这个关于套接字/网络的 Python 教程编程可能会帮助您获得所需的 Python 套接字交互(Beau Martinez 的链接似乎也很有前途。)
您还需要获得一些 PHP 套接字连接,以便它可以
继续 请求状态。 ,我的想法是,你的 Python 脚本可能会循环运行,因此,我会将“检查状态请求”检查放在该循环的一部分的开头,它会回复一个状态。而该脚本内的稍后循环将回复增加的状态..等等。
祝你好运
:我认为托马斯·舒尔茨的文件编写建议可能是最容易实现的。等待文件打开——您需要确保您的 PHP 和 Python 脚本不会挂起或返回失败而无需重试。
Unfortunately my friend, I do believe you'll need to use Sockets as you requested. :( I have little experience working with them, but This Python Tutorial on Sockets/Network Programming may help you get the Python socket interaction you need. (Beau Martinez's links seem promising as well.)
You'd also need to get some PHP socket connections, too, so it can request the status.
Continuing on that, my thoughts would be that your Python script is likely going to run in a loop. Ergo, I'd put the "Check for a status request" check inside the beginning of a part of that loop. It'd reply one status, while a later loop inside that script would reply with an increased status.. etc.
Good luck!
Edit: I think that the file writing recommendation from Thomas Schultz is probably the easiest to implement. The only downside is waiting for the file to be opened-- You'll need to make sure your PHP and Python scripts don't hang or return failure without trying again.