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 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: References: <8737sumpjl.fsf@elektro.pacujo.net> <87h9ha8lt0.fsf@jester.gateway.pace.com> <87d1rwpwo2.fsf@elektro.pacujo.net> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de vdRy/NHiQK6RJig4J8DDlQPvD2bunFNfBdQebmVjYWGg== Return-Path: 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: 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:103011 > 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 wrote: > "Marko Rauhamaa" wrote in message news:87d1rwpwo2.fsf@elektro.pacujo.net. > .. > > > > Kevin Conway : > > > > > 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 >