John Davidson

How can I make php wait until python ends

0 comments
Message:


I am using shell_exec to run python in php on MacOS


$result = shell_exec('/Users..../python parser.py '.$number.'/'.$newFileName);


php code doesn't wait until python code ends so there is no value in $result and it makes error. how can make php wait until python code ends?



My Answer:
You can use the `exec` function in PHP instead of `shell_exec` to make PHP wait until the Python code ends. The `exec` function will wait for the command to finish and return the last line of the output.

Here is an example of how you can modify your code:

php
$result = exec('/Users..../python parser.py '.$number.'/'.$newFileName);


By using `exec` instead of `shell_exec`, PHP will wait for the Python code to finish executing before moving on to the next line of code. This way, you will have the output of the Python code stored in the `$result` variable.

Rate this post

3 of 5 based on 4317 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us