简体   繁体   中英

How to have PHP display data from a program that runs asynchronously?

Question

I have a backend server program that is currently called through PHP with echo exec("python programFilePath $variables"); This method worked well, but then I added a new component to the python program. This new component makes a web request and then the necessary data is printed on the terminal output screen.

Unfortunately, the php echo exec function won't print this data, ostensibly because python's beautiful soup module runs requests asynchronously. Obviously, this breaks the webpage if a key dynamic component doesn't work.

How to make php run the program but also allow it to display the data?

Edits

Here is the code for the problematic python script I am trying to run.

It works perfectly when run through IDLE or terminal. But, when I run it with the php command:

echo exec ("python /Users/USERNAME/Desktop/Exeter_Bookstore_Project/lab/Python/amazonParser.py");

it is the only test that doesn't work on my debug page: 在此处输入图片说明

Does anyone have an idea why that is?

exec is synchronous, meaning it will wait for the process to terminate before moving the PHP script forward.

Likewise, echo will wait for the exec process to complete before outputting the results.

So if you're experiencing some short-circuit problems, the solution is likely with your Python script.

This method worked well, but then I added a new component to the python program. This new component makes the script take 2-3 seconds to run...

Maybe the component you added is running asynchronously? If so, the Python script could be returning execution to PHP immediately, then running your new component on a different thread. So from PHP's perspective, it looks like Python is finished.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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