How to place a process in the background so that it does not interrupt my return?

I need this urgently on my Django website, but due to time limitations I cannot make any heavy changes. This is probably the cheapest modification in place.

If we just focus on build or run ...

  • Now I am returning the identifier from build (or running).

  • All the hard work is now in a separate function.

 import multiprocessing as mp def main(): id = get_build_id(....) work = mp.Process(target=heavy_build_fn) work.start() return id 

If I run this in the shell (I did not test this in the Django application itself), the terminal will not end completely until the work process is completed with its task. As a web application, I need to return the identifier immediately. Is it possible to place work on the background without interruption?

Thanks.

I read this. How do I run another script in Python without waiting for it to complete? but I want to know other ways to do this, for example by sticking to MP. Popen solution Popen not be what I really want.


 import multiprocessing as mp import time def build(): print 'I build things' with open('first.txt', 'w+') as f: f.write('') time.sleep(10) with open('myname.txt', 'w+') as f: f.write('3') return def main(): build_p = mp.Process(name='build process', target=build) build_p.start() build_p.join(2) return 18 if __name__ == '__main__': v = main() print v print 'done' 

Console:

 I build things 18 done | 

and wait

finally,

 user@user-P5E-VM-DO :~$ python mp3.py I build things 18 done user@user-P5E-VM-DO :~$ 
+4
source share
2 answers

remove join() and you may have what you want. join() waits for processes to complete before returning.

The value will return until the child process (s) is completed, but the parent process will be active until the child processes are complete. Not sure if this problem is for you or not.

This code:

 import multiprocessing as mp import time def build(): print 'I build things' for i in range(10): with open('testfile{}.txt'.format(i), 'w+') as f: f.write('') time.sleep(5) def main(): build_p = mp.Process(name='build process', target=build) build_p.start() return 18 if __name__ == '__main__': v = main() print v print 'done' 

Return:

 > python mptest.py 18 done I build things 

If you need to allow process termination while the child process continues to check the answers here:

Run the process and do not wait

+3
source

No, the easiest way to handle what you want is probably to use a message broker. Django celery is a great solution. This will allow you to queue the process and return your virtual access to the user. Then your process will be executed in the order of its queue.

I believe that the process opened from Django is tied to the thread that was open, so your view will wait until it finishes.

0
source

All Articles