python - How do I place a process in the background so it does not interrupt my return? -
i need urgently in django site, because of time constraint, cannot heavy modifications. cheapest in-place modification.
if focus on either build
or run
...
now id
build
(or run).all heavy work in separate function.
'
import multiprocessing mp def main(): id = get_build_id(....) work = mp.process(target=heavy_build_fn) work.start() return id
if ran in shell (i have not tested on actual django app), terminal not end until work
process done job. web app, need return id right away. can place work
on background without interrupting?
thanks.
i've read how run script in python without waiting finish?, want know other ways it, example, sticking mp. popen
solution may not want actually.
import multiprocessing mp import time def build(): print 'i build things' open('first.txt', 'w+') f: f.write('') time.sleep(10) open('myname.txt', 'w+') 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 build things 18 done user@user-p5e-vm-do:~$
remove join()
, may have want. join()
waits processes end before returning.
the value return before child process(es) finish, however, parent process alive until child processes complete. not sure if that's issue or not.
this code:
import multiprocessing mp import time def build(): print 'i build things' in range(10): open('testfile{}.txt'.format(i), 'w+') 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'
returns:
> python mptest.py 18 done build things
if need allow process end while child process continues check out answers here:
Comments
Post a Comment