Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #102197
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Maxime S <maxischmeii@gmail.com> |
| Newsgroups | comp.lang.python |
| Subject | Re: Question about asyncio and blocking operations |
| Date | Thu, 28 Jan 2016 22:23:13 +0100 |
| Lines | 60 |
| Sender | maximesteisel@gmail.com |
| Message-ID | <mailman.65.1454016215.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> <CALwzidnGbz7kM=D7MKua2tA9-csFn9u0OHL0w-X5Bbixpcw4Ow@mail.gmail.com> <n8dgas$s1u$1@ger.gmane.org> <CALwzidn6NfT_O0cfHw1itWja81+MW3scHuEcADVCen3ix6z73w@mail.gmail.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8 |
| X-Trace | news.uni-berlin.de /7lkRJIYcuvhw4MsJeLkAQJvJf6t8n9F6HzxMz4ZZiQg== |
| Return-Path | <maximesteisel@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; 'none:': 0.05; 'subject:Question': 0.05; 'caller': 0.07; 'cc:addr:python-list': 0.09; 'buffer,': 0.09; 'cursor': 0.09; 'empty,': 0.09; 'loop.': 0.09; 'rows': 0.09; 'size)': 0.09; 'jan': 0.11; 'def': 0.13; 'received:74.125.82.44': 0.15; 'thu,': 0.15; '2016': 0.16; 'async': 0.16; 'cc:name:python': 0.16; 'deque': 0.16; 'operation,': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'sender:addr:gmail.com': 0.18; '>': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'am,': 0.23; 'header :In-Reply-To:1': 0.24; 'requests': 0.25; 'message- id:@mail.gmail.com': 0.27; 'this.': 0.28; 'blocking': 0.29; 'value)': 0.29; 'raise': 0.29; 'skip:_ 10': 0.32; 'class': 0.33; 'skip:& 20': 0.35; 'received:google.com': 0.35; 'could': 0.35; 'received:74.125.82': 0.35; 'subject:: ': 0.37; 'method': 0.37; 'self': 0.38; 'skip:s 40': 0.38; 'data': 0.39; 'some': 0.40; 'your': 0.60; 'entire': 0.61; 'provide': 0.61; '\xc2\xa0\xc2\xa0': 0.66; 'frank': 0.72; 'await': 0.76; 'actually,': 0.84 |
| 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:message-id :subject:to:cc:content-type; bh=1jyqA+VqXHhtVRWv1QjjkQrUUTpMTfOZ5WnHl4xiWAE=; b=ZjJuR+9B2JDa96+Zvl5J9ZQH2j8bERnMWkStBxcQliBAcnC+VH7vnZ5CX3Fx0LU8La t4W9tJDmvi21AT6lN5spbot2jr2PNxkxYE8LoBMTrCBLGPQMnf63HLtCHVx6cdeyDhpZ 6/gP97OKPOfjGD6z9oMuzrSOP3tIqXUKsfY7XPDYHhtsQpQY1tm5V+taa/Tsx6j9/oRI oNLc5IjFAxegPQ68dmYPjzVv9aRB3HwYR3qiGBckcUTMjnJuzsqR67Smiq+nc6Vdht/z nvR4b+/Z0pMNgEb52ya6WnPqsms8cca3SIs3eiifuzk7i0lSWGFuO49c84bAGQCvOckB /4xw== |
| X-Google-DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:sender:in-reply-to:references:from :date:message-id:subject:to:cc:content-type; bh=1jyqA+VqXHhtVRWv1QjjkQrUUTpMTfOZ5WnHl4xiWAE=; b=JoU7Kk/GhnwWs4HxlH1Befxl7qYzHnDbPM1Lt3OsghCEchwjqsBFrTy/xTlm3j8Piz jIRX/wQmzkunAhDTeMQjGWgw+JtwGRBUqsCu0eXH4Zw5GUNINI71QujEBxov4LnhIPqW MCwqG+xD9LFWMbxQWG/EBWuYAZ8Oy9ur5BR7WY+y7O1l8eytj2FAaXnCoR9OkB7z7h29 ULpGQnkqrcjcVV0Di5iLEUlW7IFfWmIGgIGRL2oVuBDfv72/Dfn9/V77Mw0bOWQz/n7j meLy66eTwrArCrFBkw/i1ze7VvkkuTqfc0CagafmKfciJDYqx8K0p7lbkhPVaNz5H4NW TqMg== |
| X-Gm-Message-State | AG10YOT/kx6WZvqi4yhbTZE/HhpqlJjab7d8t8M2UwFqJ7EcRgUyAGD1MqQeixmWF89z4tEP1N60kbH4A5hp9g== |
| X-Received | by 10.194.94.138 with SMTP id dc10mr6138288wjb.37.1454016213429; Thu, 28 Jan 2016 13:23:33 -0800 (PST) |
| In-Reply-To | <CALwzidn6NfT_O0cfHw1itWja81+MW3scHuEcADVCen3ix6z73w@mail.gmail.com> |
| X-Google-Sender-Auth | TMLlnEg8OQC2VN77JLbi6Wv9Evc |
| 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:102197 |
Show key headers only | View raw
2016-01-28 17:53 GMT+01:00 Ian Kelly <ian.g.kelly@gmail.com>:
> On Thu, Jan 28, 2016 at 9:40 AM, Frank Millman <frank@chagford.com> wrote:
>
> > The caller requests some data from the database like this.
> >
> > return_queue = asyncio.Queue()
> > sql = 'SELECT ...'
> > request_queue.put((return_queue, sql))
>
> Note that since this is a queue.Queue, the put call has the potential
> to block your entire event loop.
>
>
Actually, I don't think you actually need an asyncio.Queue.
You could use a simple deque as a buffer, and call fetchmany() when it is
empty, like that (untested):
class AsyncCursor:
"""Wraps a DB cursor and provide async method for blocking operations"""
def __init__(self, cur, loop=None):
if loop is None:
loop = asyncio.get_event_loop()
self._loop = loop
self._cur = cur
self._queue = deque()
def __getattr__(self, attr):
return getattr(self._cur, attr)
def __setattr__(self, attr, value):
return setattr(self._cur, attr, value)
async def execute(self, operation, params):
return await self._loop.run_in_executor(self._cur.execute,
operation, params)
async def fetchall(self):
return await self._loop.run_in_executor(self._cur.fetchall)
async def fetchone(self):
return await self._loop.run_in_executor(self._cur.fetchone)
async def fetchmany(self, size=None):
return await self._loop.run_in_executor(self._cur.fetchmany, size)
async def __aiter__(self):
return self
async def __anext__(self):
if self._queue.empty():
rows = await self.fetchmany()
if not rows:
raise StopAsyncIteration()
self._queue.extend(rows)
return self._queue.popleft()
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Question about asyncio and blocking operations Maxime S <maxischmeii@gmail.com> - 2016-01-28 22:23 +0100
csiph-web