Path: csiph.com!news.swapon.de!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: Mon, 15 Feb 2016 09:16:34 +0200 Lines: 35 Message-ID: References: <8737sumpjl.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 ZEQd0Qkhw3bVitspr6YeRA0UMqCNVWWx4eL8tAEZvqvg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.013 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; '"in': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'thread': 0.10; 'coroutines': 0.16; 'elsewhere,': 0.16; 'processes.': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'subject:run': 0.16; 'threads': 0.16; 'background.': 0.20; 'arguments': 0.22; 'noted': 0.22; 'wrote': 0.23; 'header:In-Reply- To:1': 0.24; 'header:X-Complaints-To:1': 0.26; 'function': 0.28; 'thread,': 0.29; 'themselves': 0.29; 'there.': 0.30; 'code': 0.30; 'task': 0.30; 'another': 0.32; "can't": 0.32; 'run': 0.33; 'class': 0.33; 'execution': 0.35; 'but': 0.36; 'there': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'turn': 0.37; 'received:org': 0.37; 'stuff': 0.38; 'or,': 0.38; 'subject:the': 0.39; 'well.': 0.40; 'to:addr:python.org': 0.40; 'some': 0.40; 'hope': 0.61; 'times': 0.63; 'benefit': 0.66; 'frank': 0.72; 'skip:n 40': 0.72; 'cpu.': 0.84; 'task,': 0.91; 'cooperative': 0.93 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: 197.89.92.141 In-Reply-To: <8737sumpjl.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:102943 "Marko Rauhamaa" wrote in message news:8737sumpjl.fsf@elektro.pacujo.net... > > "Frank Millman" : > > > Using asyncio, there are times when I want to execute a coroutine which > > is time-consuming. I do not need the result immediately, and I do not > > want to block the current task, so I want to run it in the background. > > You can't run code "in the background" using asyncio. Coroutines perform > cooperative multitasking in a single thread on a single CPU. > > Parallel processing requires the use of threads or, often preferably, > processes. > > To put it in another way, never run time-consuming code in asyncio. > No arguments there. I started with a task that ran quickly, but as I added stuff it started to slow down. The execution of the task involves calling some existing functions, which are themselves coroutines. As you have noted elsewhere, once you turn one function into a coroutine, all calls higher up the chain have to be coroutines as well. The benefit of my class is that it enables me to take the coroutine and run it in another thread, without having to re-engineer the whole thing. Hope this makes sense. Frank