Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #25684

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

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <kushal.kumaran@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; '__name__': 0.07; 'main()': 0.07; 'suppose': 0.07; 'python': 0.09; 'subject:process': 0.09; 'subject:skip:m 10': 0.09; 'subject:while': 0.09; 'underlying': 0.09; 'cc:addr:python-list': 0.10; 'looked': 0.10; 'def': 0.10; 'library': 0.15; "'__main__':": 0.16; 'descriptors': 0.16; 'hangs': 0.16; 'least,': 0.16; 'main():': 0.16; 'subject:run': 0.16; 'wrote:': 0.17; 'code,': 0.18; 'sender:addr:gmail.com': 0.18; 'app': 0.19; 'bit': 0.21; 'import': 0.21; 'cc:2**0': 0.23; 'work.': 0.23; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'am,': 0.27; 'first,': 0.27; 'skip:# 10': 0.27; 'message-id:@mail.gmail.com': 0.27; 'received:209.85.212': 0.28; 'app.': 0.29; 'queue': 0.29; 'fri,': 0.30; 'implement': 0.32; 'file': 0.32; 'running': 0.32; 'could': 0.32; 'print': 0.32; 'builds': 0.33; 'handle': 0.33; "can't": 0.34; 'skip:b 20': 0.34; 'received:google.com': 0.34; 'done': 0.34; 'especially': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'but': 0.36; 'child': 0.36; 'subject: (': 0.36; 'turn': 0.36; 'possible': 0.37; 'correctly': 0.37; 'does': 0.37; 'two': 0.37; 'being': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'things': 0.38; 'sure': 0.38; 'build': 0.39; 'little': 0.39; 'header:Received:5': 0.40; 'days': 0.60; 'john': 0.60; 'close': 0.63; 'more': 0.63; 'here': 0.65; '20,': 0.65; 'alive.': 0.65; 'jul': 0.65; 'believe': 0.69; 'compiles': 0.84; 'doable': 0.84; 'running,': 0.84; 'subject:Let': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:from:date :x-google-sender-auth:message-id:subject:to:cc:content-type; bh=KOomyX5HhvWZJZ/H2kf5auxKzvHTEvXxi+5hfz3dNPE=; b=a+pAD+G7DXwIUldG357gGK2P4drDP5Siw6hwwavLW+gtgm/8B8pi4malaNjq2/OVyp Lvz5XzNN++TFzopnYbSpGF65HuN/od7skn9NF2TTby1a9ZsQGnYTx4ZfuNzYWLMJN7c4 NbFgIigU0G9fa0o1OATcIrKcg6NVHa6qx4gKH6z+NH29uRjiJ0sW+LrJmX9faxJcfrXv wvNziox/2hkTVRaVfvuv3Ae5T+ointIVVRDuhlEufV9fUsbbQhhqZb1tOsRhvtkxL3R2 LRnL1xsE7Gx0o/gEeJNywpM0VnM8ZWLm8FoqAK9SD9RcPjLdt2UWPGCafwiXyG5sQbJZ jmfg==
MIME-Version 1.0
Sender kushal.kumaran@gmail.com
In-Reply-To <CACCLA54K9HV1qPhuT2i_dSjUk+ey3TdFN2+9x8_3Aay7de3Snw@mail.gmail.com>
References <CACCLA54K9HV1qPhuT2i_dSjUk+ey3TdFN2+9x8_3Aay7de3Snw@mail.gmail.com>
From Kushal Kumaran <kushal.kumaran+python@gmail.com>
Date Fri, 20 Jul 2012 14:18:51 +0530
X-Google-Sender-Auth _WtTmKLW7W3hEcngDfCRHHFKjrs
Subject Re: Let child process to run while parent is out (multiprocessing)
To John Wong <gokoproject@gmail.com>
Content-Type text/plain; charset=UTF-8
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2338.1342774158.4697.python-list@python.org> (permalink)
Lines 84
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1342774158 news.xs4all.nl 6844 [2001:888:2000:d::a6]:49855
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:25684

Show key headers only | View raw


On Fri, Jul 20, 2012 at 2:04 AM, John Wong <gokoproject@gmail.com> 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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Let child process to run while parent is out (multiprocessing) Kushal Kumaran <kushal.kumaran+python@gmail.com> - 2012-07-20 14:18 +0530

csiph-web