I'm trying to run a subprocess using subprocess.Popen through a batch file. When I run the batch file through the cmd it works fine. But - when I run it through the python code it executes the subprocess and does everything in the batch file but the python program that is supposed to run from the batch file doesn't seem to work.
The FastAPI endpoint:
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)
@app.post("/projects/{project_name}/run")
async def run_project(project_name: str, action: Action):
# Update the "reprocess_all" value field in the general.yaml file:
val = dict(action)['reprocess_all']
update_general_config(project_name, {'reprocess_all': val})
global bg_process
return await bg_process.run_process_project(project_name,
dict(action)['action'],
dict(action)['selected_import_sets'])
The bg_process.run_process_project():
async def run_process_project(self, project_name, action, selected_import_sets):
if action == 'start':
if self.processing_is_running:
return {'status': 'failed', 'error': 'already running', 'project_name': self.processing_project}
# ok, we can run. Create the thread to run the actual processing
# Update the 'do_process' property in the instructions.yaml:
self.update_do_process_properties(project_name, selected_import_sets)
Thread(target=self.run_process_project_thread, args=(project_name, self.websocket_queue)).start()
broadcaster_task = asyncio.create_task(self.message_broadcaster())
await broadcaster_task
return {'status': 'OK'}
def run_process_project_thread(self, project_name, websocket_queue):
# mark started
self.lock.acquire()
self.processing_status_update(run_stat=True, project_name=project_name)
self.lock.release()
print("Hey Tal")
proc = subprocess.Popen(
[fr'C:\ProgramData\axioma\gpt\run_process.bat', fr'C:\ProgramData\axioma\gpt\Projects\TalTest']
)
The batch file content:
set PYTHONPATH=c:\GPT\lib
c:\GPT\gptenv\Scripts\python.exe c:\GPT\gis\test2.py >> c:\tmp\logs.txt
When I run it through the python code it runs the batch file but not the test2.py code.
Edit:
- I forgot to mention that the code does not work only from a FastAPI endpoint. Therefore here is the code inside the function:
- Iv'e added the code running the Popen line.