简体   繁体   English

PHP和Python之间的通信

[英]Communication between PHP and Python

I'm trying to build a web interface for some python scripts. 我正在尝试为一些python脚本构建一个Web界面。 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. 问题是我必须使用PHP(而不是CGI),我执行的一些脚本需要相当长的时间才能完成:5-10分钟。 Is it possible for PHP to communicate with the scripts and display some sort of progress status? PHP是否可以与脚本通信并显示某种进度状态? 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. 目前使用exec()并在完成时我处理输出。 The server is running on a Windows machine, so pcntl_fork will not work. 服务器在Windows机器上运行,因此pcntl_fork将无法运行。

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) 稍后编辑 :使用另一个PHP脚本使用ajax提供主页信息似乎不起作用,因为服务器杀死它(它达到最大执行时间,我真的不想增加它,除非必要)

I was thinking about socket based communication but I don't see how is this useful in my case (some hints, maybe? 我正在考虑基于套接字的通信,但我不知道在我的情况下这有什么用处(一些提示,也许?

Thank you 谢谢

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. 你需要设置一个套接字来监听 PHP中的连接(在同一台机器上),并设置一个套接字连接到Python中的监听套接字并将其状态发送给它。

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) . 从Python文档和Python socket模块的文档(特别是最后的示例)中查看此套接字编程概述 I'm sure PHP has similar resources. 我确信PHP有类似的资源。

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). 一旦您对要构建的内容有了更具体的了解并需要帮助,请随意在StackOverflow上提出一个问题(如果尚未回答)。

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. 我认为你必须使用元刷新,并且可能让python将状态写入文件,然后从中读取php。

You could use AJAX as well to make it more dynamic. 您也可以使用AJAX使其更具动态性。

Also, probably shouldn't use exec()...that opens up a world of vulnerabilities. 此外,可能不应该使用exec()......这会打开一个漏洞世界。

You could use a queuing service like Gearman , with a client in PHP and a worker in Python or vice versa. 您可以使用像Gearman这样的排队服务,PHP中的客户端和Python中的工作者,反之亦然。

Someone has created an example setup here. 有人在这里创建了一个示例设置。

https://github.com/dbaltas/gearman-python-worker https://github.com/dbaltas/gearman-python-worker

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.) :(我没有使用它们的经验,但是这个关于套接字/网络编程的Python教程可能会帮助你获得所需的Python套接字交互。(Beau Martinez的链接似乎也很有希望。)

You'd also need to get some PHP socket connections, too, so it can request the status. 您还需要获得一些PHP套接字连接,以便它可以请求状态。

Continuing on that, my thoughts would be that your Python script is likely going to run in a loop. 接下来,我的想法是你的Python脚本可能会在循环中运行。 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. 编辑:我认为Thomas Schultz的文件撰写建议可能是最容易实现的。 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. 唯一的缺点是等待打开文件 - 您需要确保您的PHP和Python脚本不会挂起或返回失败而不再尝试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM