I need this urgently in my Django site, but because of the time constraint, I cannot do any heavy modifications. This is probably the cheapest in-place modification.
If we just focus on either build
or run
...
Now I get the id back from
build
(or run).All the heavy 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 ran this in the shell (I have not tested this on the actual Django app), the terminal will not end completely until work
process is done with its job. As a web app, I need to return the id right away. Can I place work
on the background without interrupting?
Thanks.
I've read this How do I run another script in Python without waiting for it to finish?, but I want to know other ways to do it, for example, sticking with MP. The Popen
solution may not be what I want actually.
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:~$