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


Groups > comp.lang.python > #102193 > unrolled thread

Re: Question about asyncio and blocking operations

Started byIan Kelly <ian.g.kelly@gmail.com>
First post2016-01-28 09:53 -0700
Last post2016-01-28 09:53 -0700
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

#102193 — Re: Question about asyncio and blocking operations

FromIan Kelly <ian.g.kelly@gmail.com>
Date2016-01-28 09:53 -0700
SubjectRe: Question about asyncio and blocking operations
Message-ID<mailman.63.1454000074.2338.python-list@python.org>
On Thu, Jan 28, 2016 at 9:40 AM, Frank Millman <frank@chagford.com> wrote:
> I have hit a snag. It feels like a bug in 'await q.get()', though I am sure
> it is just me misunderstanding how it works.
>
> I can post some working code if necessary, but here is a short description.
>
> Here is the database handler - 'request_queue' is a queue.Queue -
>
>        while not request_queue.empty():
>            return_queue, sql = request_queue.get()
>            cur.execute(sql)
>            for row in cur:
>                return_queue.put_nowait(row)
>             return_queue.put_nowait(None)
>            request_queue.task_done()

As I commented in my previous message, asyncio.Queue is not
thread-safe, so it's very important that the put calls here be done on
the event loop thread using event_loop.call_soon_threadsafe. This
could be the cause of the strange behavior you're seeing in getting
the results.

> 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.

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web