Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #102179
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Subject | Re: Question about asyncio and blocking operations |
| Date | Thu, 28 Jan 2016 22:36:57 +1100 |
| Lines | 42 |
| Message-ID | <mailman.48.1453981020.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 9y8FOk/v0ruN20tIlqkhlwzjjCTwHkSS7BbH6PcobZpA== |
| Return-Path | <rosuav@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; 'socket': 0.07; 'cc:addr :python-list': 0.09; 'block.': 0.09; 'blocked': 0.09; 'covered.': 0.09; 'oh,': 0.09; 'second.': 0.09; 'tuning': 0.09; 'jan': 0.11; '(at': 0.13; 'thu,': 0.15; "(it's": 0.16; '2016': 0.16; 'async': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'sequencing': 0.16; 'simulate': 0.16; 'spit': 0.16; 'wrote:': 0.16; 'instance,': 0.18; 'creates': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'seems': 0.23; 'header:In-Reply-To:1': 0.24; 'requests': 0.25; '(which': 0.26; 'message-id:@mail.gmail.com': 0.27; 'back.': 0.27; '(maybe': 0.29; "people's": 0.29; 'queue': 0.29; 'request,': 0.29; 'second,': 0.29; 'creating': 0.30; "i'd": 0.31; 'probably': 0.31; 'point': 0.33; 'problem': 0.33; 'lock': 0.33; 'stream': 0.33; 'case,': 0.34; 'this?': 0.34; 'gets': 0.35; 'received:google.com': 0.35; 'could': 0.35; 'easiest': 0.35; 'fail': 0.35; 'instance': 0.35; 'tasks': 0.35; 'something': 0.35; 'but': 0.36; 'should': 0.36; 'received:209.85': 0.36; '(and': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:209.85.213': 0.37; 'things': 0.38; "won't": 0.38; 'received:209': 0.38; 'wrong': 0.38; 'end': 0.39; 'enough': 0.39; 'takes': 0.39; 'waiting': 0.60; 'your': 0.60; 'maximum': 0.61; "you'll": 0.61; 'entire': 0.61; 'back': 0.62; 'safe': 0.63; 'monitoring': 0.64; 'limit': 0.65; 'request.': 0.66; 'real-world': 0.66; "they're": 0.66; 'risk': 0.68; 'frank': 0.72; '"it': 0.84; '10:11': 0.84; 'chrisa': 0.84; 'suspicion': 0.84; 'to:none': 0.91; 'instantly': 0.93; 'room,': 0.93 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=7S3BrhDjBiEIx7SthvDWE/ePKpfnLTWrarspFmVtU8k=; b=N4ucWxrA+ctJUZnv133PyGKv3Bi18iFyxD62SQDIzmMAKRhH+47aBl1s5IxyAOtqpk yxwrjjMLxRuGJqacUY0tcDB9Rphb2/RgQNeAhur0Mi5QBFbdi1sNhspnNhAPqGJcNwUo fYNTPHkDbe0tG9FhU0AauvLalM3IfF0hxdQPYZQX8eE4ZgOtlqa9kdGSlpdgRd4DoELr +mhmC1c1z8nYxRByPoUPGFy6g2tGvNVyjwFPrVr5PhSGDMPVcnN4PhzenBlrc62Gz5pd TD/1kNRqfqfeK3gQh5wCvt0+FgrS707CIcfRyQRVGfQAIEUt1D0mygcRG1zl1EX9we9s HIAw== |
| 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:date :message-id:subject:from:cc:content-type; bh=7S3BrhDjBiEIx7SthvDWE/ePKpfnLTWrarspFmVtU8k=; b=OMO2ZXrfj5rijkmVj67T/LiMTEfBaK0kFHQorvInqLua8oKpemMSpIC7iMiq6C3Tyu 5EXbhLqN/ojgrR6NxizLN3fvYZ6gWCyqt5NEtOBZvXevws+tA+Ti4jtBPRFEynK4jFyw qo67Mjt7Fdd91wkhvjj1k8sdCClJ6NhVTMuu2bLXCiFngU18NX8NCgzltGk5Od4VZOqa HYRSRnvbq2kK0nwP5F/ARBTOjh4OnhdGLHqvf85OUfiqsazv0TNz8erQr65aLjfsaHT8 ogN+yhFutT5a1Rm1EIkFK6BU5sQJV7v6TV0MXbQavVK06M5idyuTOV2aFVYFoxYVKL3b BVWw== |
| X-Gm-Message-State | AG10YOSK+czEAASRoSqXRxt8TLE7b3sBG98kEPctSk92MMezuWuhYLFz+5CTgjAHlC0vDYxFupJMBWmB5ZwjNQ== |
| X-Received | by 10.50.111.169 with SMTP id ij9mr2859378igb.92.1453981017173; Thu, 28 Jan 2016 03:36:57 -0800 (PST) |
| In-Reply-To | <n8ct18$fu4$1@ger.gmane.org> |
| 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:102179 |
Show key headers only | View raw
On Thu, Jan 28, 2016 at 10:11 PM, Frank Millman <frank@chagford.com> wrote: >> >> 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? Oh, I get it. In that case, you should be safe (at the cost of efficiency, presumably, but probably immeasurably so). The easiest way to thrash-test this is to simulate an ever-increasing stream of requests that eventually get bottlenecked by the database. For instance, have the database "process" a maximum of 10 requests a second, and then start feeding it 11 requests a second. Monitoring the queue length should tell you what's happening. Eventually, the queue will hit its limit (and you can make that happen sooner by creating the queue with a small maxsize), at which point the queue.put() will block. My suspicion is that that's going to lock your entire backend, unlocking only when the database takes something off its queue; you'll end up throttling at the database's rate (which is fine), but with something perpetually blocked waiting for the database (which is bad - other jobs, like socket read/write, won't be happening). As an alternative, you could use put_nowait or put(item, block=False) to put things on the queue. If there's no room, it'll fail immediately, which you can spit back to the user as "Database overloaded, please try again later". Tuning the size of the queue would then be an important consideration for real-world work; you'd want it long enough to cope with bursty traffic, but short enough that people's requests don't time out while they're blocked on the database (it's much tidier to fail them instantly if that's going to happen). ChrisA
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Question about asyncio and blocking operations Chris Angelico <rosuav@gmail.com> - 2016-01-28 22:36 +1100
csiph-web