Home > Mobile >  How to wait for a python script to complete run and then download the resulting file with PHP?
How to wait for a python script to complete run and then download the resulting file with PHP?

Time:02-11

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. The idea is to access that zip file through an URL, using PHP.

But the main issue here is PHP making the zip available, even before the file is not yet completely compressed.

Using the 'sleep' function, I believe will halt the execution of PHP, even the Python script, am I right?

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.

Is there any way of doing all the process within PHP script, without using Windows Task Scheduler?

<?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 the code I am using for the whole process.

CodePudding user response:

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
  • The PHP script loop on read the file while the end time is not set. And after delete the file. And then, it takes the zip.

CodePudding user response:

Why not call the PHP from Python at the end of the py script? 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()
  • Related