开发者

Communication between PHP and Python

开发者 https://www.devze.com 2023-03-04 22:37 出处:网络
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 possibl

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


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).


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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消