Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Let child process to run while parent is out (multiprocessing)

Reply
Thread Tools

Re: Let child process to run while parent is out (multiprocessing)

 
 
Kushal Kumaran
Guest
Posts: n/a
 
      07-20-2012
On Fri, Jul 20, 2012 at 2:04 AM, John Wong <> wrote:
> def main(...):
> build_id = create_build_id(...)
> build_stuff
> return build_id
>
> Suppose build_stuff compiles a C program. It could take days to finish, and
> notify users their builds are ready. I was thinking about using
> mutliprocessing to handle the build_stuff.
>
> So here is a sample:
>
> #!/usr/bin/python
>
> import multiprocessing as mp
> import time
>
> def build():
> print 'I am building HUGE things'
> time.sleep(10)
>
> def main():
> build_p = mp.Process(name='build process', target=build)
> build_p.start()
> return 'abcd12345'
>
> if __name__ == '__main__':
>
> v = main()
> print v
> print 'done'
>
> Here is output:
> yeukhon@fermat:~$ python c2.py
> abcd12345
> done [now hangs for 10 seconds]
> I build things
>
> When I looked at `ps -elf|grep python`, I can see two processes running, and
> one of the python c2.py process is `wait`. But this is not ideal,
> especially this is a web app. I can't implement any advanced queue / event
> system right now (I use Pylon, I believe I have gevent installed). But just
> with multiprocessing, is it possible to send the id first, while running
> child in the backgroud?
>
> Right now it hangs there as long as the child process is alive. Any
> solutions?
>


>From the documentation, there does not seem to be any way of

"detaching" a multiprocessing Process. But it is doable by using the
underlying os.fork directly (CAUTION: not ready for being invoked from
a web app):

#!/usr/bin/python

import os
import time

def build_and_send_email():
print 'I am building HUGE things'
time.sleep(10)

def main():
child_pid = os.fork()
if child_pid == 0:
build_and_send_email()
os._exit(0)
return 'abcd12345'

if __name__ == '__main__':
v = main()
print v
print 'done'

To make it work correctly with web app will require a bit more work.
At the least, you will have to close all file descriptors to make sure
the request processing finishes. You can turn it into a proper
background process (a daemon) using the python-daemon library with
very little code, I think.

--
regards,
kushal
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Let child process to run while parent is out (multiprocessing) Chris Angelico Python 0 07-19-2012 11:05 PM
If a class Child inherits from Parent, how to implementChild.some_method if Parent.some_method() returns Parent instance ? metal Python 8 10-30-2009 10:31 AM
a simple program to illustrate that child process does not shareresources with parent process sree.harsha.sn@gmail.com C Programming 4 11-09-2008 03:59 AM
Pass from parent to child, then update parent with child value... Noel Dolan Javascript 0 07-18-2004 05:52 PM
how do I let a process started via Runtime.exec() run after the parent thread returns? rabbits77 Java 1 02-25-2004 09:08 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57