Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Israel Brewster Newsgroups: comp.lang.python Subject: Re: Bi-directional sub-process communication Date: Mon, 23 Nov 2015 14:14:11 -0900 Lines: 102 Message-ID: References: <5A3A00A6-CFD2-42FC-9C87-6F8C6861F775@ravnalaska.net> <20151123214516.GA56871@cskk.homeip.net> Mime-Version: 1.0 (Mac OS X Mail 9.1 \(3096.5\)) Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable X-Trace: news.uni-berlin.de YuRETY6RqE9S2CJ9j12CZwSuvszxBLfqw3a/tO/5+3Ww== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'url:sourceforge': 0.03; 'receives': 0.03; 'essentially': 0.04; 'memory.': 0.05; 'that?': 0.05; '"as': 0.07; 'option,': 0.07; 'subject:skip:c 10': 0.07; 'cc:addr:python-list': 0.09; '3),': 0.09; 'callback': 0.09; 'cherrypy': 0.09; 'global,': 0.09; 'subject:process': 0.09; 'threads,': 0.09; 'thread': 0.10; ':-)': 0.12; '23,': 0.16; 'cc:name:python': 0.16; 'cheap.': 0.16; 'follow-up': 0.16; 'limit,': 0.16; 'processor,': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:sub': 0.16; 'thread.': 0.16; 'threading': 0.16; 'threads': 0.16; 'wrote:': 0.16; 'memory': 0.17; 'implementing': 0.18; 'variable': 0.18; 'all,': 0.20; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; '(or': 0.23; 'second': 0.24; 'implemented': 0.24; 'thus': 0.24; 'header :In-Reply-To:1': 0.24; 'mon,': 0.24; 'requests': 0.25; 'sort': 0.25; 'chris': 0.26; 'checking': 0.27; 'handling': 0.27; 'processed': 0.27; 'turns': 0.27; 'correct': 0.28; 'function': 0.28; 'cpu': 0.29; 'dictionary': 0.29; 'once,': 0.29; 'thread,': 0.29; 'allows': 0.30; 'that.': 0.30; 'url:mailman': 0.30; 'code': 0.30; 'checks': 0.30; 'probably': 0.31; 'option': 0.31; "can't": 0.32; 'implement': 0.32; 'generally': 0.32; 'maybe': 0.33; 'getting': 0.33; 'run': 0.33; 'useful': 0.33; 'url:python': 0.33; 'call,': 0.33; "i'll": 0.33; 'url:listinfo': 0.34; 'case,': 0.34; 'running': 0.34; 'could': 0.35; 'i.e.': 0.35; 'nov': 0.35; 'something': 0.35; 'step': 0.36; 'but': 0.36; 'there': 0.36; 'url:org': 0.36; 'cases': 0.36; 'child': 0.36; 'data.': 0.36; 'depends': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'agree': 0.37; 'method': 0.37; 'client': 0.37; 'available.': 0.37; 'charset:us-ascii': 0.37; 'doing': 0.38; 'itself': 0.38; 'means': 0.39; 'goes': 0.39; 'data': 0.39; 'does': 0.39; 'subject:-': 0.39; 'rather': 0.39; 'resources': 0.39; 'url:mail': 0.40; 'still': 0.40; 'some': 0.40; 'easy': 0.60; 'waiting': 0.60; 'avoid': 0.61; 'header:Message-Id:1': 0.61; 'provide': 0.61; 'back': 0.62; 'per': 0.62; 'skip:n 10': 0.62; 'making': 0.62; 'course': 0.62; 'more': 0.63; 'information': 0.63; 'times': 0.63; 'between': 0.65; 'benefit': 0.66; 'request.': 0.66; 'received:10.9': 0.66; 'reply': 0.68; 'reply,': 0.72; 'sounds': 0.76; 'received:12': 0.81; 'child,': 0.84; 'dispatched': 0.84; 'replies.': 0.84; 'single,': 0.84; 'tie': 0.84; 'viable': 0.84; 'wakes': 0.84 X-Warning: RFC compliance checks disabled due to whitelist X-Warning: Reverse-Path DNS check skipped due to whitelist X-Warning: Maximum message size check skipped due to whitelist X-Warning: Realtime Block Lists skipped due to whitelist X-Warning: System filters skipped due to whitelist X-Warning: Domain filters skipped due to whitelist X-Warning: User filters skipped due to whitelist X-Warning: Anti-Spam check skipped due to whitelist X-Whitelist: 2147483645 X-Envelope-From: israel@ravnalaska.net X-Envelope-To: python-list@python.org In-Reply-To: X-Mailer: Apple Mail (2.3096.5) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:99287 On Nov 23, 2015, at 1:43 PM, Chris Kaynor = wrote: >=20 > On Mon, Nov 23, 2015 at 2:18 PM, Israel Brewster = > wrote: >=20 >> Of course, that last step could be interesting - implementing the = block in >> such a way as to not tie up the processor, while still getting the = data "as >> soon" as it is available. Unless there is some sort of built-in >> notification system I could use for that? I.e. the thread would = "subscribe" >> to a notification based on its tag, and then wait for notification. = When >> the master processing thread receives data with said tag, it adds it = to the >> dictionary and "publishes" a notification to that tag. Or perhaps the >> notification itself could contain the payload? >=20 >=20 > There are a few ways I could see handling this, without having the = threads > spinning and consuming CPU: >=20 > 1. Don't worry about having the follow-up code run in the same = thread, > and use a simple callback. This callback could be dispatched to a = thread > via a work queue, however you may not get the same thread as the one = that > made the request. This is probably the most efficient method to use, = as the > threads can continue doing other work while waiting for a reply, = rather > than blocking. It does make it harder to maintain state between the = pre- > and post-request functions, however. > 2. Have a single, global, event variable that wakes all threads = waiting > on a reply, each of which then checks to see if the reply is for it, = or > goes back to sleep. This is good if most of the time, only a few = threads > will be waiting for a reply, and checking if the correct reply came = in is > cheap. This is probably good enough, unless you have a LOT of = threads > (hundreds). > 3. Have an event per thread. This will use less CPU than the second > option, however does require more memory and OS resources, and so = will not > be viable for huge numbers of threads, though if you hit the limit, = you are > probably using threads wrong. > 4. Have an event per request. This is only better than #3 if a = single > thread may make multiple requests at once, and can do useful work = when any > of them get a reply back (if they need all, it will make no = difference). >=20 > Generally, I would use option #1 or #2. Option 2 has the advantage of > making it easy to write the functions that use the functionality, = while > option 1 will generally use fewer resources, and allows threads to = continue > to be used while waiting for replies. How much of a benefit that is = depends > on exactly what you are doing. While I would agree with #1 in general, the threads, in this case, are = CherryPy threads, so I need to get the data and return it to the client = in the same function call, which of course means the thread needs to = block until the data is ready - it can't return and let the result be = processed "later". Essentially there are times that the web client needs some information = that only the Child process has. So the web client requests the data = from the master process, and the master process then turns around and = requests the data from the child, but it needs to get the data back = before it can return it to the web client. So it has to block waiting = for the data. Thus we come to option #2 (or 3), which sounds good but I have no clue = how to implement :-) Maybe something like http://pubsub.sourceforge.net = ? I'll dig into that. >=20 > Option #4 would probably be better implemented using option #1 in all = cases > to avoid problems with running out of OS memory - threading features > generally require more limited OS resources than memory. Option #3 = will > also often run into the same issues as option #4 in the cases it will > provide any benefit over option #2. >=20 > Chris > --=20 > https://mail.python.org/mailman/listinfo/python-list