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 14:49:25 -0700 Lines: 28 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de YtLcgrFJuUKGTFxaXgTbfQDZ3uARTERDEusT7YLRZYGw== Return-Path: 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; 'subject:Question': 0.05; 'caller': 0.07; 'concurrently': 0.07; 'executed': 0.07; 'wrapper': 0.07; 'appropriate.': 0.09; 'buffer,': 0.09; 'cursor': 0.09; 'empty,': 0.09; 'loop.': 0.09; 'underlying': 0.09; 'jan': 0.11; 'thu,': 0.15; '2016': 0.16; 'deque': 0.16; 'producer': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'suggestion.': 0.16; 'threads': 0.16; 'wrote:': 0.16; 'am,': 0.23; 'seems': 0.23; 'header:In-Reply-To:1': 0.24; 'requests': 0.25; 'message- id:@mail.gmail.com': 0.27; 'this.': 0.28; 'interface': 0.29; 'queue': 0.29; 'true.': 0.33; 'received:google.com': 0.35; 'could': 0.35; "isn't": 0.35; 'received:209.85': 0.36; 'to:addr :python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'received:209.85.213': 0.37; 'received:209': 0.38; 'data': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'your': 0.60; 'entire': 0.61; 'different': 0.63; 'frank': 0.72; 'actually,': 0.84; '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=8eYYGuoZ9ByOUkmNfxvzinE7LTIG3OjmvmphM4e8wxg=; b=wR1o2l6Yts4pmVU4vRcg3AAzR4PR3NmzJXAoxzbGxSqz3d+b9rKDul7EPWGeZo6a17 kK4gg3BSQ6n0ixK6lqnm/ALiDkWI+ju8uPbjZ55pIgmb39svmmmiNfuSJgbyWlc6CAXy J5ofBPhajmA4uMwM8Z//eZfz5LarSCuGM9HSXGD/6rxlBAsUZUjXflftH+0RfxXCFT0f Fz8YWWzp24uBvGl01CrZgaddPiyKqO/ojHvkVKs54jVooF5m2anQ5YtcTly3Oz4BoMrF ZQxYjzVWIfil5Gr8ptBoQu6IekQXDAyjMwFjYy2FiSuxehyZGxhSLSQH5+lwlPlhfY9k WJNQ== 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=8eYYGuoZ9ByOUkmNfxvzinE7LTIG3OjmvmphM4e8wxg=; b=fIjbqk54OekVmOOIM+IUd0+iICdowNLVyAFhjDzjBvmyn73lfq0rjV+E06sv2lAk29 aJmjy7ocH0903DWPr4mHWKmAfeoX/C1RW4aggHn9zHUC+jnwcxWntTtRgAH4xMlB1Mum SHSCnXsiVSrUUeYuHQlt5Dfva6eyg4uky3XGQLsk+xENmiICrMy+buIJrIcLSIeK+Gh+ FLg8RRtm1q4z95vat3WuXkEGUsi484DzZ3kInBAEBoXqXoRuw8KggxBLIcd1WuW6DZj5 j1kLUrA8TdfezBis5lCUDHfGd7NecWCvmwFfxUdeAVSHSiSO3hJSJT3uSsEE5U6YlT6F Irew== X-Gm-Message-State: AG10YOS/mDcch2psTwK5jvREUJrngM14x7D0RqoiG4c7UB3FWuD6lQwOlt/cTwJAbSLUhi/qgiCb0eWURwTTJw== X-Received: by 10.50.102.40 with SMTP id fl8mr6058927igb.85.1454017805253; Thu, 28 Jan 2016 13:50:05 -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:102198 On Thu, Jan 28, 2016 at 2:23 PM, Maxime S wrote: > > 2016-01-28 17:53 GMT+01:00 Ian Kelly : >> >> On Thu, Jan 28, 2016 at 9:40 AM, Frank Millman 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): True. The asyncio Queue is really just a wrapper around a deque with an interface designed for use with the producer-consumer pattern. If the producer isn't a coroutine then it may not be appropriate. This seems like a nice suggestion. Caution is advised if multiple cursor methods are executed concurrently since they would be in different threads and the underlying cursor may not be thread-safe.