简体   繁体   English

如何等待 python 脚本完成运行,然后使用 PHP 下载生成的文件?

[英]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:澄清过程:

  1. An application needs to access the file I am downloading with the python script应用程序需要访问我正在使用 python 脚本下载的文件
  2. I feed the application with the URL from Apache server我从 Apache 服务器向应用程序提供 URL
  3. the application goes to the link (through index.php) and triggers the python script (which downloads the zip file)应用程序转到链接(通过 index.php)并触发 python 脚本(下载 zip 文件)
  4. php should pop-up a save file WHEN the zip file has been completely downloaded php应该弹出一个保存文件当zip文件已经完全下载

May be you can do something like that:也许你可以做这样的事情:

  • The python script put at the begining a file somewhere with informations like start time and end time when it endded python 脚本在文件的开头放置了一个文件,其中包含开始时间和结束时间等信息
  • The PHP script loop on read the file while the end time is not set. PHP 脚本在未设置结束时间时循环读取文件。 And after delete the file.删除文件后。 And then, it takes the zip.然后,它需要 zip。

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.

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