How to make a parallel system call in python?

Asked

Viewed 527 times

3

I’m looking to run a program parallel to mine, in case it’s called download.py, it must run independent of my main execution main.py, but should be called in the same. I thought of making systems call and am using the subprocess, as in the example below:

 ...
 path = '/home/prisvo/view_admin/backend/update_article/download.py'         
 process = subprocess.call(['python', path], stdout=PIPE, stderr=PIPE)
 ...       

However, I want to continue with the execution of the code normally, the execution of the download.py it takes a while, but I can’t wait for the execution. Is there any function of the Subprocess or another library that can do this.

1 answer

4


You can use threads to execute processes in pararelo.

I will put here the simplest example depending on the code you provided:

import threading
import time # por motivos de demonstracao

def download(): # codigo relativo ao download
    # path = '/home/prisvo/view_admin/backend/update_article/download.py'         
    # process = subprocess.call(['python', path], stdout=PIPE, stderr=PIPE)
    time.sleep(5) # so por demo
    print('download acabou')

threading.Thread(target=download).start() # iniciar o processo em paralelo
# continuar o resto da logica
print('aqui o codigo continua')
print('aqui tambem')
print('e aqui também')

DEMONSTRATION

Note: in this example I do not expect any feedback from function download, if I had to work on something that could return from the function I would have to structure it a little differently (queue, example)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.