How to make a parallel system call in python?

3

I want to run a program parallel to mine, in case it is called download.py , it should run independently of my main main.py execution, but it should be called in it. I thought about calling systems and I'm using 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 running the code normally, running download.py takes a while, but I can not wait for execution. Is there any function of Subprocess or other library that can do this.

    
asked by anonymous 04.09.2018 / 18:04

1 answer

4

You can use threads to execute processes on it.

I'll put the simplest example here 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 return of the download function, if I had to work on something that could return from the function I would have to structure it a bit differently ( queue , #

04.09.2018 / 18:25