1

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

The output through the cmd: enter image description here

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.
Tl2
  • 61
  • 6
  • Try: `["cmd.exe", "/k", fr'C:\ProgramData\axioma\gpt\run_process.bat', fr'C:\ProgramData\axioma\gpt\Projects\TalTest']` – Maurice Meyer Mar 07 '23 at 14:16
  • @MauriceMeyer That doesn't seem to work. I've updated the post noting that the code doesn't do the job only from a FastAPI endpoint. – Tl2 Mar 08 '23 at 06:29
  • Have you tried passing the infamous `Shell=True` parameter to `subprocess.Popen`? – Friedrich Mar 08 '23 at 06:36
  • 1
    Yes I tried that and it didn't work. @Friedrich – Tl2 Mar 08 '23 at 06:39
  • This is probably an environment problem. You should add the command `set` into your batch and search for the differences when launched from the command line and from the FastAPI handler. – Serge Ballesta Mar 08 '23 at 07:22
  • On a side note, please have a look at [this answer](https://stackoverflow.com/a/71517830/17865804) to understand the difference between `async def` and `def` in FastAPI – Chris Mar 08 '23 at 07:36
  • @Chris Iv'e added the code lines so you can see. As for the enviroment variables - there is a set to the PYTHONPATH for the enviroment variable. Is there another env variable that I should add? – Tl2 Mar 08 '23 at 09:10
  • @Tl2: If I had known what the problem was exactly, I would have posted an answer... I just advised you to do what I would have done first if I was facing that problem. – Serge Ballesta Mar 08 '23 at 09:19
  • @SergeBallesta The same code works on another computer so I guess the problem is with my local computer. – Tl2 Mar 08 '23 at 09:36

0 Answers0