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


Groups > comp.lang.python > #102188

Re: Question about asyncio and blocking operations

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Ian Kelly <ian.g.kelly@gmail.com>
Newsgroups comp.lang.python
Subject Re: Question about asyncio and blocking operations
Date Thu, 28 Jan 2016 08:33:17 -0700
Lines 56
Message-ID <mailman.58.1453995246.2338.python-list@python.org> (permalink)
References <n8038j$575$1@ger.gmane.org> <n8818q$35e$1@ger.gmane.org> <CALwzidk-RBkB-vi6CgcEeoFHQrsoTFvqX9MqzDD=rnY5bOCRUg@mail.gmail.com> <n8aln3$fah$1@ger.gmane.org> <CALwzidn6TvN9W-2qnn2JYvJu8NHzn499nPtfjn9OHjdDcebVbA@mail.gmail.com> <CALwzidmmUeL3Sq9o6ZgKYM3-V22HCshG03wQqsXvhh6xy1otgQ@mail.gmail.com> <CALwzidkr-fT6S6wH2caNaxyQvUdAw=x7XDqkqOfNRrwzwnjvOQ@mail.gmail.com> <n8cm4k$ojk$1@ger.gmane.org> <CAPTjJmr162+K4LZeFpXruR6wxrHxbR-_wkrCLLDyR7kST+kjYg@mail.gmail.com> <n8ct18$fu4$1@ger.gmane.org>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de 1YDyX/6/wVxdDcZ6HiVbkAte8rV2ya1g9zAKICs49GAw==
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.005
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'handler': 0.04; 'subject:Question': 0.05; 'caller': 0.07; 'covered.': 0.09; 'handler.': 0.09; 'loop.': 0.09; 'rows': 0.09; 'assume': 0.11; 'jan': 0.11; 'thu,': 0.15; '2016': 0.16; 'async': 0.16; 'coroutines': 0.16; 'operation.': 0.16; 'queuing': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'sequencing': 0.16; 'skip:n 70': 0.16; 'think?': 0.16; 'thread.': 0.16; 'threads': 0.16; 'wrote:': 0.16; '&gt;': 0.18; 'creates': 0.18; 'extension': 0.20; 'issue.': 0.20; "aren't": 0.22; 'am,': 0.23; 'seems': 0.23; 'wrote': 0.23; 'header:In-Reply-To:1': 0.24; 'requests': 0.25; 'figure': 0.27; 'separate': 0.27; 'message-id:@mail.gmail.com': 0.27; 'back.': 0.27; '(maybe': 0.29; 'block,': 0.29; 'blocking': 0.29; 'queue': 0.29; 'request,': 0.29; 'that.': 0.30; 'work.': 0.30; "i'd": 0.31; 'good.': 0.32; 'run': 0.33; 'problem': 0.33; 'case,': 0.34; 'this?': 0.34; 'gets': 0.35; 'received:google.com': 0.35; 'could': 0.35; 'important.': 0.35; 'instance': 0.35; 'tasks': 0.35; 'something': 0.35; 'but': 0.36; 'should': 0.36; 'instead': 0.36; 'received:209.85': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'being': 0.37; 'one,': 0.37; 'received:209.85.213': 0.37; "won't": 0.38; 'received:209': 0.38; 'wrong': 0.38; 'test': 0.39; 'data': 0.39; 'sure': 0.39; 'easily': 0.39; 'to:addr:python.org': 0.40; 'waiting': 0.60; 'share': 0.61; 'real': 0.62; 'back': 0.62; 'more': 0.63; 'request.': 0.66; 'results': 0.66; 'future.': 0.67; 'risk': 0.68; 'feeling': 0.72; 'frank': 0.72; '.....': 0.76; '"it': 0.84; '&quot;it': 0.84; 'gut': 0.84; 'to:name:python': 0.84; 'unclear': 0.84
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; bh=iJmMIp8t0wgimCWvstslkS1RRYiR+xkPdeIXYUJiMak=; b=YhoViBBH7j7fS6uCjACcUeWJEryC4pDS1ogsaXbXZRmKEztqXUuV34jOSZGjcux0gO KDrTDepenAxmvH2Efib9fnu/KhjgGsg545Mb8eYFD9tW0cWj/sFFMM1OzO4dgERDKGnm arTFj3UYATU8HRjFAgRqVVRlaaeWsLW81zGz4Baqw4n+3A/d1liMGN4iC8bGEQJyDHn/ sapfz2t1eL84d1GPkFaSEVdNUDNmr+gO3VsWigAGL6kSllQ7t4xP8JOcpJKksLlRyQRB arxLJbUMXySTPLT1tEE47exR+sIXKmZYAMYgVo5WnS9Azcnawq3EuJacBuSz8QQDF4AS Yajw==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:content-type; bh=iJmMIp8t0wgimCWvstslkS1RRYiR+xkPdeIXYUJiMak=; b=PRr/mtZd7g2PmH4S+9F5wX1oa7VA2Vx7WoT36OB9PGwiFmugkO1Qy4z16T4qAKCf3A UJ3cNXA3FcFRjd/fxA8VAvbxoAEoMuqoD7j/Ps9QKjZ2QihMZXVTO4KxR1bdaxqRVbk+ nwSlCoec4CCJZF2FjFIachFEQl/JbHJEWS/n+mk/VwrUtuxqs7jckPOY3icb05y7pDG9 z3mLiT5EI7L0YK3alsrpv2mj4m+Y/gY7nBMnxbpHiJqCBMrLQSVylIJU0Qu2ElLcXWMz hiL7lbT5IL/r6uEFbl7/1nz6vZqHpeflr4ZY59iYP0mCKvQU4LTGgyQFyASf3j/eNual fHoA==
X-Gm-Message-State AG10YOQNUdSZciQuYdXtd1a2jXEXBd3O0y9/Y6ipaSGMxPwJesQ/En1qpbh4DOgeY87+er5cCa0W5H7oHUb3Wg==
X-Received by 10.50.43.168 with SMTP id x8mr4095368igl.93.1453995237022; Thu, 28 Jan 2016 07:33:57 -0800 (PST)
In-Reply-To <n8ct18$fu4$1@ger.gmane.org>
X-Content-Filtered-By Mailman/MimeDel 2.1.20+
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>
Xref csiph.com comp.lang.python:102188

Show key headers only | View raw


On Jan 28, 2016 4:13 AM, "Frank Millman" <frank@chagford.com> wrote:
>
> "Chris Angelico"  wrote in message
news:CAPTjJmr162+K4LZeFpXruR6wxrHxbR-_wkrCLLDyR7kST+kjYg@mail.gmail.com...
>>
>>
>> On Thu, Jan 28, 2016 at 8:13 PM, Frank Millman <frank@chagford.com>
wrote:
>> > Run the database handler in a separate thread. Use a queue.Queue to
send
>> > requests to the handler. Use an asyncio.Queue to send results back to
> the
>> > caller, which can call 'await q.get()'.
>> >
>> > I ran a quick test and it seems to work. What do you think?
>>
>> My gut feeling is that any queue can block at either get or put ....
>>
>
> H'mm, I will have to think about that one, and figure out how to create a
worst-case scenario. I will report back on that.

The get and put methods of asyncio queues are coroutines, so I don't think
this would be a real issue. The coroutine might block, but it won't block
the event loop. If the queue fills up, then effectively the waiting
coroutines just become a (possibly unordered) extension of the queue.

>>
>> The other risk is that the wrong result will be queried (two async
>> tasks put something onto the queue - which one gets the first
>> result?), which could either be coped with by simple sequencing (maybe
>> this happens automatically, although I'd prefer a
>> mathematically-provable result to "it seems to work"), or by wrapping
>> the whole thing up in a function/class.
>>
>
> I *think* I have this one covered. When the caller makes a request, it
creates an instance of an asyncio.Queue, and includes it with the request.
The db handler uses this queue to send the result back.
>
> Do you see any problem with this?

That seems reasonable to me. I assume that when you send the result back
you would be queuing up individual rows and not just sending a single
object across, which could be more easily with just a single future.

The main risk of adding limited threads to an asyncio program is that
threads make it harder to reason about concurrency. Just make sure the
threads don't share any state and you should be good.

Note that I can only see queues being used to move data in this direction,
not in the opposite. It's unclear to me how queue.get would work from the
blocking thread. Asyncio queues aren't threadsafe, but you couldn't just
use call_soon_threadsafe since the result is important. You might want to
use a queue.Queue instead in that case, but then you run back into the
problem of queue.put being a blocking operation.

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


Thread

Re: Question about asyncio and blocking operations Ian Kelly <ian.g.kelly@gmail.com> - 2016-01-28 08:33 -0700

csiph-web