Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: "Frank Millman" Newsgroups: comp.lang.python Subject: Re: asyncio - run coroutine in the background Date: Tue, 16 Feb 2016 16:36:00 +0200 Lines: 36 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; format=flowed; charset="iso-8859-1"; reply-type=original Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de GwaGg2oAGZk8G09J7oE8ews2lVHI3pvGkJNEQRp0qFFw== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'run,': 0.07; 'concurrent': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'throw': 0.09; 'thread': 0.10; 'consume': 0.16; 'coroutines': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'run.': 0.16; 'simulated': 0.16; 'subject:run': 0.16; 'thread.': 0.16; 'background.': 0.20; 'wrote': 0.23; 'attach': 0.23; 'kevin': 0.23; 'header:In-Reply-To:1': 0.24; "doesn't": 0.26; 'header:X -Complaints-To:1': 0.26; 'handling': 0.27; 'separate': 0.27; 'function': 0.28; 'about.': 0.29; 'argue': 0.29; 'cpu': 0.29; 'handled': 0.29; 'task': 0.30; 'seconds': 0.31; 'users.': 0.31; 'option': 0.31; 'another': 0.32; '[1]': 0.32; 'run': 0.33; 'problem': 0.33; 'facility': 0.33; 'could': 0.35; 'done': 0.35; 'tasks': 0.35; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'does': 0.39; 'subject:the': 0.39; 'rather': 0.39; '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; 'task,': 0.91; 'good,': 0.93 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: 197.89.68.71 In-Reply-To: <87d1rwpwo2.fsf@elektro.pacujo.net> X-MSMail-Priority: Normal Importance: Normal X-Newsreader: Microsoft Windows Live Mail 15.4.3502.922 X-MimeOLE: Produced By Microsoft MimeOLE V15.4.3502.922 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:103010 "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