Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ian Kelly Newsgroups: comp.lang.python Subject: Re: Question about asyncio and blocking operations Date: Thu, 28 Jan 2016 09:53:51 -0700 Lines: 30 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de kB2ugKhaX/ALKYSiMHrMuAGg58Dr2JAL2jzdbglHmZPQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'received:209.85.223': 0.03; 'handler': 0.04; 'subject:Question': 0.05; 'caller': 0.07; 'works.': 0.07; 'loop.': 0.09; 'bug': 0.10; 'thread': 0.10; 'jan': 0.11; 'thu,': 0.15; '2016': 0.16; 'commented': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'row': 0.16; 'wrote:': 0.16; 'am,': 0.23; 'header:In-Reply-To:1': 0.24; 'requests': 0.25; 'message-id:@mail.gmail.com': 0.27; 'skip:e 30': 0.27; 'this.': 0.28; "skip:' 10": 0.28; 'necessary,': 0.29; 'code': 0.30; 'post': 0.31; 'getting': 0.33; 'previous': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'done': 0.35; 'but': 0.36; 'received:209.85': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:209': 0.38; 'data': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'your': 0.60; 'behavior': 0.61; 'entire': 0.61; 'strange': 0.63; 'here': 0.66; 'results.': 0.67; 'frank': 0.72; 'to:name:python': 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=YGip0dqOT3bQ4IUXzq9jeZamBAxrWlwVNLGGByBclMI=; b=V9+vEgns9HegU8B6RqhTDKpcCXu4B3Gd93st3a6yw92nqxU1crRvtZ1+wR+qYdCp+F YM1UoQwsgJAZ5qCq0qVQbwmk79uezqOD6AGVMSlxY+CE8YGQuWoBRlrfOB4p0JTQWQPh EIDijYOFl6ztt3XkFyq3dhLsudcHF4JrO7jzOmd6LEA8AStXaAt50alFyUbAVlf1hFN7 l2P70BeGenu3E5IPYYSzA3rgu2rvG3f1Y2kRAz9DZaB7irF+JxjAHz44liapa2eAIybv 9/BKH4O9nZje+y5bnMO0cvPIqwki7EXAD5K37kGD+uGhDNtFDaug3BXExXVPtHIHmlDz 0fRw== 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=YGip0dqOT3bQ4IUXzq9jeZamBAxrWlwVNLGGByBclMI=; b=hqIcJ8rMQxhZoMF+vBcS6qod5FpwGcsjRtOM0MsSI4WET1WWcPLzzyENrWsEAKVGdF 5EvNzZ3AozZruTgIRRSijUh/VoqjJ8C7HJDc9ueIBoslftgsE3H9dPjr4vRCKVB3fQIS JT/iTZicGhPRI69HDo02f7KRMUyfJfkYSBO3qMQRQ44WTO3L7vx80h9oxmxJ/ljAYEvg t4SaaToV2y1kEEs5Kav8wZP67oykJrD5pNtzlB6zEBhR9bFsGnuMcrhKzfPvsi/3K3p6 ye8nnt3zYv1bRQIk71geyqxzLZXbJ1GfAZrz290BWFRboM8yiKpx8CUHgVhbXiyhy0Ik FunQ== X-Gm-Message-State: AG10YOSVu6i5CLjkK4k2YLww1JSzY7Q/WFpPl2YCnsGCns/nbpDIvunmtcK6xBZyORs3Uayu7IDkSRbbdkJVQw== X-Received: by 10.107.11.68 with SMTP id v65mr4979032ioi.188.1454000071324; Thu, 28 Jan 2016 08:54:31 -0800 (PST) In-Reply-To: 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:102193 On Thu, Jan 28, 2016 at 9:40 AM, Frank Millman 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.