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


Groups > comp.lang.python > #99280

Re: Bi-directional sub-process communication

Path csiph.com!news.mixmin.net!weretis.net!feeder4.news.weretis.net!ecngs!testfeeder.ecngs.de!81.171.118.63.MISMATCH!peer03.fr7!news.highwinds-media.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Ian Kelly <ian.g.kelly@gmail.com>
Newsgroups comp.lang.python
Subject Re: Bi-directional sub-process communication
Date Mon, 23 Nov 2015 12:55:49 -0700
Lines 61
Message-ID <mailman.74.1448308612.2291.python-list@python.org> (permalink)
References <B819AD72-C0F9-4E6C-992A-5E5556979883@ravnalaska.net>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
X-Trace news.uni-berlin.de jwiAp/Itdi5O23PMvkGn5g8V5JIOawPujAKOmkB+56Tg==
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.006
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'one?': 0.05; 'subject:skip:c 10': 0.07; 'any)': 0.09; 'concern:': 0.09; 'subject:process': 0.09; 'thread': 0.10; 'def': 0.13; 'acquired': 0.15; '10:54': 0.16; '23,': 0.16; 'order?': 0.16; 'reads,': 0.16; 'received:209.85.213.176': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:sub': 0.16; 'threads': 0.16; 'true:': 0.16; 'wrote:': 0.16; 'say,': 0.18; '2015': 0.20; 'prevent': 0.20; 'trying': 0.22; 'am,': 0.23; 'seems': 0.23; 'passing': 0.23; 'this:': 0.23; 'header:In-Reply-To:1': 0.24; 'mon,': 0.24; 'requests': 0.25; 'order.': 0.27; 'possibility': 0.27; 'message-id:@mail.gmail.com': 0.27; 'this.': 0.28; 'fine': 0.28; 'directly,': 0.29; 'locks': 0.29; 'queue': 0.29; 'objects': 0.29; 'raise': 0.29; 'option': 0.31; 'post': 0.31; 'another': 0.32; 'posting': 0.32; 'class': 0.33; 'third': 0.33; 'lock': 0.33; 'handle': 0.34; 'that,': 0.34; 'received:google.com': 0.35; 'so,': 0.35; 'next': 0.35; 'could': 0.35; 'nov': 0.35; 'something': 0.35; 'but': 0.36; 'should': 0.36; 'instead': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'possible': 0.36; 'child': 0.36; 'to:addr :python-list': 0.36; 'subject:: ': 0.37; 'received:209.85.213': 0.37; 'starting': 0.37; 'received:209': 0.38; 'wrong': 0.38; 'enough': 0.39; 'subject:-': 0.39; 'to:addr:python.org': 0.40; 'called': 0.40; 'waiting': 0.60; 'care': 0.60; 'side': 0.62; 'back': 0.62; 'more': 0.63; 'complete': 0.63; 'request.': 0.66; 'note:': 0.66; 'guaranteed': 0.67; 'therefore': 0.67; 'sole': 0.76; 'to:name:python': 0.84; 'responses': 0.93
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; bh=uzmB5k0xrzTT952PUqB4zgxhFozP6xzg/NuctN+nNqY=; b=FH2R/6NyLifwXYGyn1L7STGXqMvo/PiMgsTQoHaZCGL7mVU0bOwgNQwm2EtbLtwDnQ wXWg2klU6hZw2coD1GuMIBINsxkFbK2urons1UH79ciVvU6MIsVK47vWdVbwu9JUejgt o9alYZkYG7oWjP1YiTn/IZQ3E+h0uJzTnilrmDNQUjYroDuBp0CQ2Rc7QBtACVQl9Twt QeIzq3ZJFhje2qhiE3E1RV25DGOHvRPNEjt20WX9p3QNLYMuivvz3LMdD+SLcjoHInBf nUgIEiBfBc1IDQV63nlXYY/wgPkt1LOQfSeU72h7B7V8WTl/SGXvUh4babfRjU7QcdI6 EjvA==
X-Received by 10.50.78.231 with SMTP id e7mr13939979igx.93.1448308588734; Mon, 23 Nov 2015 11:56:28 -0800 (PST)
In-Reply-To <B819AD72-C0F9-4E6C-992A-5E5556979883@ravnalaska.net>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
X-Received-Bytes 6435
X-Received-Body-CRC 3151208339
Xref csiph.com comp.lang.python:99280

Show key headers only | View raw


On Mon, Nov 23, 2015 at 10:54 AM, Israel Brewster <israel@ravnalaska.net> wrote:
> Concern: Since the master process is multi-threaded, it seems likely enough that multiple threads on the master side would make requests at the same time. I understand that the Queue class has locks that make this fine (one thread will complete posting the message before the next is allowed to start), and since the child process only has a single thread processing messages from the queue, it should process them in order and post the responses (if any) to the master_queue in order. But now I have multiple master processes all trying to read master_queue at the same time. Again, the locks will take care of this and prevent any overlapping reads, but am I guaranteed that the threads will obtain the lock and therefore read the responses in the right order? Or is there a possibility that, say, thread three will get the response that should have been for thread one? Is this something I need to take into consideration, and if so, how?

Yes, if multiple master threads are waiting on the queue, it's
possible that a master thread could get a response that was not
intended for it. As far as I know there's no guarantee that the
waiting threads will be woken up in the order that they called get(),
but even if there are, consider this case:

Thread A enqueues a request.
Thread B preempts A and enqueues a request.
Thread B calls get on the response queue.
Thread A calls get on the response queue.
The response from A's request arrives and is given to B.

Instead of having the master threads pull objects off the response
queue directly, you might create another thread whose sole purpose is
to handle the response queue. That could look like this:


request_condition = threading.Condition()
response_global = None

def master_thread():
    global response_global
    with request_condition:
        request_queue.put(request)
        request_condition.wait()
        # Note: the Condition should remain acquired until
response_global is reset.
        response = response_global
        response_global = None
    if wrong_response(response):
        raise RuntimeError("got a response for the wrong request")
    handle_response(response)

def response_thread():
    global response_global
    while True:
        response = response_queue.get()
        with request_condition:
            response_global = response
            request_condition.notify()


As another option you could use a multiprocessing.Manager to
coordinate passing the response back more directly, but starting a
third process seems like overkill for this.

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


Thread

Re: Bi-directional sub-process communication Ian Kelly <ian.g.kelly@gmail.com> - 2015-11-23 12:55 -0700

csiph-web