Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #103011
| Path | csiph.com!feeder.erje.net!2.eu.feeder.erje.net!newsfeed.kamp.net!newsfeed.kamp.net!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Kevin Conway <kevinjacobconway@gmail.com> |
| Newsgroups | comp.lang.python |
| Subject | Re: asyncio - run coroutine in the background |
| Date | Tue, 16 Feb 2016 14:54:04 +0000 |
| Lines | 60 |
| Message-ID | <mailman.172.1455634489.22075.python-list@python.org> (permalink) |
| References | <mailman.131.1455518141.22075.python-list@python.org> <8737sumpjl.fsf@elektro.pacujo.net> <mailman.132.1455520607.22075.python-list@python.org> <87h9ha8lt0.fsf@jester.gateway.pace.com> <CAPTjJmoWUDfcOe3OCaMK7=zDtW3QzS+7hqUG6DoYZ2AbqzJBUw@mail.gmail.com> <mailman.166.1455628969.22075.python-list@python.org> <87d1rwpwo2.fsf@elektro.pacujo.net> <n9vc4j$n89$1@ger.gmane.org> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8 |
| X-Trace | news.uni-berlin.de vdRy/NHiQK6RJig4J8DDlQPvD2bunFNfBdQebmVjYWGg== |
| Return-Path | <kevinjacobconway@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.009 |
| X-Spam-Evidence | '*H*': 0.98; '*S*': 0.00; '16,': 0.03; 'run,': 0.07; 'concurrent': 0.09; 'throw': 0.09; 'thread': 0.10; 'async': 0.16; 'bound.': 0.16; 'consume': 0.16; 'coroutines': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'run.': 0.16; 'simulated': 0.16; 'subject:run': 0.16; 'thread.': 0.16; 'threads': 0.16; 'wrote:': 0.16; 'driver': 0.18; '>': 0.18; 'background.': 0.20; 'to:2**1': 0.21; "aren't": 0.22; 'feb': 0.23; 'wrote': 0.23; 'attach': 0.23; 'kevin': 0.23; 'header:In-Reply- To:1': 0.24; "doesn't": 0.26; 'handling': 0.27; 'separate': 0.27; 'message-id:@mail.gmail.com': 0.27; 'function': 0.28; 'about.': 0.29; 'argue': 0.29; 'cpu': 0.29; 'handled': 0.29; 'url:mailman': 0.30; 'work.': 0.30; 'code': 0.30; 'task': 0.30; 'seconds': 0.31; 'supposed': 0.31; 'users.': 0.31; 'option': 0.31; 'another': 0.32; '[1]': 0.32; 'run': 0.33; 'problem': 0.33; 'url:python': 0.33; 'facility': 0.33; 'url:listinfo': 0.34; 'tue,': 0.34; 'running': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'done': 0.35; 'primarily': 0.35; 'tasks': 0.35; 'something': 0.35; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'url:org': 0.36; 'received:209.85': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'skip:& 10': 0.37; 'received:209': 0.38; 'wrong': 0.38; 'does': 0.39; 'subject:the': 0.39; 'received:209.85.214': 0.39; 'rather': 0.39; 'url:mail': 0.40; 'to:addr:python.org': 0.40; 'future': 0.60; 'waiting': 0.60; 'your': 0.60; 'times': 0.63; 'delegate': 0.66; 'situation': 0.67; 'frank': 0.72; 'skip:n 40': 0.72; 'affected.': 0.84; 'land,': 0.84; 'task,': 0.91; 'good,': 0.93 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :content-type; bh=Bl1wXsTfSfgas2WNS22wLEx0QkdyFee/QRy/R5lyWFA=; b=RNirL8ldkfz+e9CknVT60O8QpPBb/5F5L1pBASte+mdtXGvhrUqWLlU6mCbsrp7tRG p31j/VaBaruoBpM7un65kFiXyjTxVhNon9N7cAZ+TundcadU/U9OWneX/NI6isP4Nq3R 2xIUw+DYGWlX55Y807o7dZzXqXZdY47ab8+mlCau+kkyhphA2kykcr/bc4wbE7TJFuYr lH9/DBbvN5oslFDiC3ALq6QgfjF1+FCnDuiwYhCAgLrmu1mcwyOGwd/jyYEVRSyrgW4o bNMo/bRHSYGovMyfP6dzWk10PzVKtTDc+9COeEROy1ncbhlTC1rhOkXtYbA9TnhLlHJ1 gn9A== |
| X-Google-DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:content-type; bh=Bl1wXsTfSfgas2WNS22wLEx0QkdyFee/QRy/R5lyWFA=; b=RY2Z69VsJAhSSt+VyB7QD5C+BoCSqA6vpSON2fHKEQz2JtsFKC4rRy59VAlR6Ivuaa 07ry9K9Ys0Nd/fKlCt2rzbh0qPkNU96jWjLbwXE3FFjT8J9NQscCeGq+tkeh/vJ4Gl45 4czpmCCi5nRzQINXNok0AMTHLyo7RKRg1kDPL0GmqwTdxjMhrNON2hJj7NpQWqWwSYUt EjTTKAdTEJq1hT4UnMu90eiwJkJMiGA/9SU27H1wONWjULhd5phy2lx4uXV9yu9AswZ5 ASL0qH4wcMvUxztu8YauFFBl/DXXWrX67xi+OxYxh36NUo7HcN/5UldctCnKta7NFHgv vLzQ== |
| X-Gm-Message-State | AG10YOQMDaavYnBouxbAMoX/bse2vcBZ4H9mwgztzz2akNOtGbqHXgic+jWApOZnyUX/LrtCgeUSSNCA5zVetQ== |
| X-Received | by 10.202.71.203 with SMTP id u194mr16167650oia.5.1455634453924; Tue, 16 Feb 2016 06:54:13 -0800 (PST) |
| In-Reply-To | <n9vc4j$n89$1@ger.gmane.org> |
| X-Content-Filtered-By | Mailman/MimeDel 2.1.21rc2 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.21rc2 |
| 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:103011 |
Show key headers only | View raw
> Ok, yes, but those "background tasks" monopolize the CPU once they are scheduled to run. This is true if the coroutines are cpu bound. If that is the case then a coroutine is likely the wrong choice for that code to begin with. Coroutines, in asyncio land, are primarily designed for io bound work. > My background task does take a long time to run - about 10 seconds - but most of that time is spent waiting for database responses, which is handled in another thread. Something else to look into is an asyncio driver for your database connections. Threads aren't inherently harmful, but using them to achieve async networking when running asyncio is a definite code smell since that is precisely the problem asyncio is supposed to solve for. On Tue, Feb 16, 2016, 08:37 Frank Millman <frank@chagford.com> wrote: > "Marko Rauhamaa" wrote in message news:87d1rwpwo2.fsf@elektro.pacujo.net. > .. > > > > Kevin Conway <kevinjacobconway@gmail.com>: > > > > > If you're handling coroutines there is an asyncio facility for > > > "background tasks". The ensure_future [1] will take a coroutine, > > > attach it to a Task, and return a future to you that resolves when the > > > coroutine is complete. > > > > Ok, yes, but those "background tasks" monopolize the CPU once they are > > scheduled to run. > > > > If your "background task" doesn't need a long time to run, just call the > > function in the foreground and be done with it. If it does consume time, > > you need to delegate it to a separate process so the other tasks remain > > responsive. > > > > I will explain my situation - perhaps you can tell me if it makes sense. > > My background task does take a long time to run - about 10 seconds - but > most of that time is spent waiting for database responses, which is handled > in another thread. > > You could argue that the database thread should rather be handled by > another > process, and that is definitely an option if I find that response times are > affected. > > So far my response times have been very good, even with database activity > in > the background. However, I have not simulated a large number of concurrent > users. That could throw up the kinds of problem that you are concerned > about. > > Frank > > > -- > https://mail.python.org/mailman/listinfo/python-list >
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
asyncio - run coroutine in the background "Frank Millman" <frank@chagford.com> - 2016-02-15 08:35 +0200
Re: asyncio - run coroutine in the background Marko Rauhamaa <marko@pacujo.net> - 2016-02-15 08:54 +0200
Re: asyncio - run coroutine in the background "Frank Millman" <frank@chagford.com> - 2016-02-15 09:16 +0200
Re: asyncio - run coroutine in the background Marko Rauhamaa <marko@pacujo.net> - 2016-02-15 09:34 +0200
Re: asyncio - run coroutine in the background Paul Rubin <no.email@nospam.invalid> - 2016-02-14 23:39 -0800
Re: asyncio - run coroutine in the background "Frank Millman" <frank@chagford.com> - 2016-02-15 10:17 +0200
Re: asyncio - run coroutine in the background Marko Rauhamaa <marko@pacujo.net> - 2016-02-15 13:05 +0200
Re: asyncio - run coroutine in the background Chris Angelico <rosuav@gmail.com> - 2016-02-16 16:51 +1100
Re: asyncio - run coroutine in the background Kevin Conway <kevinjacobconway@gmail.com> - 2016-02-16 13:22 +0000
Re: asyncio - run coroutine in the background Marko Rauhamaa <marko@pacujo.net> - 2016-02-16 16:17 +0200
Re: asyncio - run coroutine in the background "Frank Millman" <frank@chagford.com> - 2016-02-16 16:36 +0200
Re: asyncio - run coroutine in the background Kevin Conway <kevinjacobconway@gmail.com> - 2016-02-16 14:54 +0000
Re: asyncio - run coroutine in the background Steven D'Aprano <steve@pearwood.info> - 2016-02-17 02:17 +1100
Re: asyncio - run coroutine in the background Marko Rauhamaa <marko@pacujo.net> - 2016-02-16 19:12 +0200
Re: asyncio - run coroutine in the background Paul Rubin <no.email@nospam.invalid> - 2016-02-17 20:38 -0800
Re: asyncio - run coroutine in the background Marko Rauhamaa <marko@pacujo.net> - 2016-02-18 08:10 +0200
Re: asyncio - run coroutine in the background Marko Rauhamaa <marko@pacujo.net> - 2016-02-16 19:13 +0200
Re: asyncio - run coroutine in the background Marko Rauhamaa <marko@pacujo.net> - 2016-02-16 19:14 +0200
Re: asyncio - run coroutine in the background Marko Rauhamaa <marko@pacujo.net> - 2016-02-16 19:15 +0200
Re: asyncio - run coroutine in the background Marko Rauhamaa <marko@pacujo.net> - 2016-02-16 19:15 +0200
Re: asyncio - run coroutine in the background Robin Becker <robin@reportlab.com> - 2016-02-16 17:52 +0000
Re: asyncio - run coroutine in the background "Frank Millman" <frank@chagford.com> - 2016-02-16 17:21 +0200
Re: asyncio - run coroutine in the background Marko Rauhamaa <marko@pacujo.net> - 2016-02-16 19:20 +0200
Re: asyncio - run coroutine in the background Paul Rubin <no.email@nospam.invalid> - 2016-02-19 23:40 -0800
Re: asyncio - run coroutine in the background Marko Rauhamaa <marko@pacujo.net> - 2016-02-20 10:13 +0200
Re: asyncio - run coroutine in the background Paul Rubin <no.email@nospam.invalid> - 2016-02-20 00:37 -0800
Re: asyncio - run coroutine in the background Chris Angelico <rosuav@gmail.com> - 2016-02-20 19:52 +1100
Re: asyncio - run coroutine in the background Marko Rauhamaa <marko@pacujo.net> - 2016-02-20 10:59 +0200
Re: asyncio - run coroutine in the background Chris Angelico <rosuav@gmail.com> - 2016-02-20 20:02 +1100
Re: asyncio - run coroutine in the background Marko Rauhamaa <marko@pacujo.net> - 2016-02-20 11:28 +0200
Re: asyncio - run coroutine in the background Kevin Conway <kevinjacobconway@gmail.com> - 2016-02-20 13:52 +0000
Re: asyncio - run coroutine in the background "Martin A. Brown" <martin@linux-ip.net> - 2016-02-20 09:45 -0800
Re: asyncio - run coroutine in the background Chris Angelico <rosuav@gmail.com> - 2016-02-21 08:47 +1100
Re: asyncio - run coroutine in the background Chris Angelico <rosuav@gmail.com> - 2016-02-17 02:28 +1100
Re: asyncio - run coroutine in the background "Frank Millman" <frank@chagford.com> - 2016-02-16 17:45 +0200
Re: asyncio - run coroutine in the background "Frank Millman" <frank@chagford.com> - 2016-02-16 15:52 +0200
csiph-web