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


Groups > comp.lang.python > #26340

Re: Pass data to a subprocess

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeder2.ecngs.de!ecngs!feeder.ecngs.de!xlned.com!feeder1.xlned.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <andrea.crotti.0@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'frameworks': 0.05; 'memory.': 0.05; 'sys': 0.05; '__name__': 0.07; 'correct.': 0.07; 'exit': 0.07; 'nasty': 0.07; 'python': 0.09; 'fork': 0.09; 'thread,': 0.09; 'url:github': 0.09; 'cc:addr :python-list': 0.10; 'def': 0.10; 'anyway': 0.11; '%d:': 0.16; "'__main__':": 0.16; 'daemons': 0.16; 'forks.': 0.16; 'idx': 0.16; 'idx,': 0.16; 'itertools': 0.16; 'pipes': 0.16; 'processes.': 0.16; 'return,': 0.16; 'run(self):': 0.16; 'self.daemon': 0.16; 'subprocess': 0.16; 'thanks,': 0.18; 'code,': 0.18; 'module': 0.19; 'import': 0.21; 'cc:2**0': 0.23; 'cc:no real name:2**0': 0.24; 'idea': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply- To:1': 0.25; 'structures': 0.27; 'message-id:@mail.gmail.com': 0.27; 'run': 0.28; 'queue': 0.29; 'servers.': 0.29; 'sleep': 0.29; 'surprised': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; 'usually': 0.30; 'worked': 0.30; 'code': 0.31; 'running': 0.32; 'could': 0.32; 'done,': 0.33; 'subject:data': 0.33; 'another': 0.33; "can't": 0.34; 'received:google.com': 0.34; 'wrong': 0.34; 'process,': 0.35; 'expected': 0.35; 'similar': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'really': 0.36; 'except': 0.36; 'but': 0.36; 'child': 0.36; 'anything': 0.36; 'should': 0.36; 'skip:p 20': 0.36; 'does': 0.37; 'option': 0.37; 'uses': 0.37; 'communicate': 0.37; 'received:209': 0.37; 'far': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'skip:l 20': 0.38; 'things': 0.38; 'instead': 0.39; 'header:Received:5': 0.40; 'think': 0.40; 'your': 0.60; 'most': 0.61; 'share': 0.61; 'between': 0.63; 'production': 0.63; 'effective': 0.63; 'more': 0.63; 'here': 0.65; 'talking': 0.66; 'child,': 0.84; 'killing': 0.84; 'nagy': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=mb+x7lNwsCCcJqHnfTFej9eY1tULgF2EmzumYqZpE9o=; b=KKBgwIW7mFoJASVB1mdICGl4yLGIp9vMGVXF+rDrek0TZqqy5Mas/MAJJH+IuIEjfj prYAbTcs3lmDvgvgeVIqf/hUE8a/xro1nKgedFGkH9YIWnSzQk89VAo2yyCtc1oVeH5P V8jvHbZtewZRLmg4us8CUYP2OURIGYqbOJa1S5jq7MkmWdCIWkxrqvltEp+vWVgfg8w5 GZbu5kaUrLz+at7FegC5dIq6CIYJcx97Reu0R+E0153BRZs0bcBNRsFGF8yxFI2Hxdan jTxuALWR42wq2yysTIkGwUwP6tuBWOA5cxzBQheFrT8YpyGzlHLPmjfhtQ1dY+GsD7S1 DHhA==
MIME-Version 1.0
In-Reply-To <5018E687.9000105@shopzeus.com>
References <CAF_E5JY5FgEJub7tNNzZEj0tri8Z7oPZUSLOLtD+5f0t7Yv+Yg@mail.gmail.com> <CAF_E5JY2sEsauXM-teW4hTHePqFCYK6z+9oLMZcjVWFyYs+gKg@mail.gmail.com> <5017EFB0.6080608@shopzeus.com> <CAF_E5JaEHxsjKoHRhZLzh+YdPU8UqKHae9efCsP7zEvm_37+2w@mail.gmail.com> <5018E687.9000105@shopzeus.com>
Date Wed, 1 Aug 2012 10:50:38 +0100
Subject Re: Pass data to a subprocess
From andrea crotti <andrea.crotti.0@gmail.com>
To Laszlo Nagy <gandalf@shopzeus.com>
Content-Type text/plain; charset=ISO-8859-1
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.2811.1343814640.4697.python-list@python.org> (permalink)
Lines 71
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1343814640 news.xs4all.nl 6924 [2001:888:2000:d::a6]:44930
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:26340

Show key headers only | View raw


2012/8/1 Laszlo Nagy <gandalf@shopzeus.com>:
>> I was just surprised that it worked better than I expected even
>> without Pipes and Queues, but now I understand why..
>>
>> Anyway now I would like to be able to detach subprocesses to avoid the
>> nasty code reloading that I was talking about in another thread, but
>> things get more tricky, because I can't use queues and pipes to
>> communicate with a running process that it's noit my child, correct?
>>
> Yes, I think that is correct. Instead of detaching a child process, you can
> create independent processes and use other frameworks for IPC. For example,
> Pyro.  It is not as effective as multiprocessing.Queue, but in return, you
> will have the option to run your service across multiple servers.
>
> The most effective IPC is usually through shared memory. But there is no OS
> independent standard Python module that can communicate over shared memory.
> Except multiprocessing of course, but AFAIK it can only be used to
> communicate between fork()-ed processes.


Thanks, there is another thing which is able to interact with running
processes in theory:
https://github.com/lmacken/pyrasite

I don't know though if it's a good idea to use a similar approach for
production code, as far as I understood it uses gdb..  In theory
though I could be able to set up every subprocess with all the data
they need, so I might not even need to share data between them.

Anyway now I had another idea to avoid to be able to stop the main
process without killing the subprocesses, using multiple forks.  Does
the following makes sense?  I don't really need these subprocesses to
be daemons since they should quit when done, but is there anything
that can go wrong with this approach?

from os import fork
from time import sleep
from itertools import count
from sys import exit

from multiprocessing import Process, Queue

class LongProcess(Process):
    def __init__(self, idx, queue):
        Process.__init__(self)
        # self.daemon = True
        self.queue = queue
        self.idx = idx

    def run(self):
        for i in count():
            self.queue.put("%d: %d"  % (self.idx, i))
            print("adding %d: %d"  % (self.idx, i))
            sleep(2)


if __name__ == '__main__':
    qu = Queue()

    # how do I do a multiple fork?
    for i in range(5):
        pid = fork()
        # if I create here all the data structures I should still be
able to do things
        if pid == 0:
            lp = LongProcess(1, qu)
            lp.start()
            lp.join()
            exit(0)
        else:
            print("started subprocess with pid ", pid)

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


Thread

Re: Pass data to a subprocess andrea crotti <andrea.crotti.0@gmail.com> - 2012-08-01 10:50 +0100

csiph-web