[英]How to wait for a python script to complete run and then download the resulting file with PHP?
I have a python script (on Windows Server) that requests information through an API, downloads data into several csv and at the end it 'zips' them all into 1 single.zip file.我有一个 python 脚本(在 Windows 服务器上)通过 API 请求信息,将数据下载到多个 csv 中,最后将它们全部“压缩”到 1 个 single.zip 文件中。 The idea is to access that zip file through an URL, using PHP.这个想法是通过 URL,使用 PHP 来访问 zip 文件。
But the main issue here is PHP making the zip available, even before the file is not yet completely compressed.但这里的主要问题是 PHP 使 zip 可用,甚至在文件尚未完全压缩之前。
Using the 'sleep' function, I believe will halt the execution of PHP, even the Python script, am I right?使用“睡眠”function,我相信会停止执行 PHP,甚至 Python 脚本,对吗?
My current setting is using a Task, on Windows Task Scheduler, to trigger the Python script, download the necessary files and only then, making the zip file available with PHP.我当前的设置是在 Windows 任务计划程序上使用任务来触发 Python 脚本,下载必要的文件,然后才使 zip 文件可用于 PHP。
Is there any way of doing all the process within PHP script, without using Windows Task Scheduler?有没有办法在 PHP 脚本中完成所有过程,而不使用 Windows 任务计划程序?
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . "/#classes/simple_html_dom.php";
#header("Content-type: text/html");
header("Content-Disposition: attachment; filename=api-week-values.zip");
unlink("api-week-values.zip");
$command = escapeshellcmd('python api_weekly.py');
$output = shell_exec($command);
sleep(15);
?>
You can see the code I am using for the whole process.你可以看到我在整个过程中使用的代码。
To clarify the process:澄清过程:
May be you can do something like that:也许你可以做这样的事情:
Why not call the PHP from Python at the end of the py script?为什么不在py脚本末尾从Python调用PHP呢? Link 关联
import subprocess
# if the script don't need output.
subprocess.call("php /path/to/your/script.php")
# if you want output
proc = subprocess.Popen("php /path/to/your/script.php", shell=True, stdout=subprocess.PIPE)
script_response = proc.stdout.read()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.