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 ...
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 :~$
source share