Path: csiph.com!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 13:22:36 +0000 Lines: 60 Message-ID: References: <8737sumpjl.fsf@elektro.pacujo.net> <87h9ha8lt0.fsf@jester.gateway.pace.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de yv+wL/VD7fu/KQnngb9p+QkZyXAU/F1kV0eyLQhiI8rw== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'cpython': 0.05; 'concurrently': 0.07; 'mentioned,': 0.07; 'socket': 0.07; 'api': 0.09; 'cc:addr:python-list': 0.09; 'thread': 0.10; 'python': 0.10; 'def': 0.13; 'interpreter': 0.15; '2016': 0.16; 'coroutines': 0.16; 'happily': 0.16; 'numpy': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:run': 0.16; 'threading': 0.16; 'threads': 0.16; 'true:': 0.16; 'wrote:': 0.16; 'obviously': 0.16; '>': 0.18; 'email addr:gmail.com>': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'function,': 0.22; 'feb': 0.23; 'attach': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'mon,': 0.24; 'paul': 0.24; 'chris': 0.26; 'handling': 0.27; 'skip:t 40': 0.27; 'message-id:@mail.gmail.com': 0.27; 'executing': 0.27; 'function': 0.28; 'blocking': 0.29; 'cpu': 0.29; 'gil': 0.29; 'running.': 0.29; 'thread,': 0.29; 'read,': 0.29; 'url:mailman': 0.30; 'work.': 0.30; 'code': 0.30; '15,': 0.30; 'another': 0.32; "can't": 0.32; '[1]': 0.32; 'run': 0.33; 'class': 0.33; 'url:python': 0.33; 'facility': 0.33; 'limitations': 0.33; 'url:listinfo': 0.34; 'schedule': 0.34; 'that,': 0.34; 'received:google.com': 0.35; 'done': 0.35; 'but': 0.36; 'there': 0.36; 'url:org': 0.36; 'received:209.85': 0.36; '(and': 0.36; 'depends': 0.36; 'limitation': 0.36; 'url:library': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'being': 0.37; 'skip:& 10': 0.37; 'release': 0.37; 'doing': 0.38; 'received:209': 0.38; 'drop': 0.38; 'subject:the': 0.39; 'received:209.85.214': 0.39; 'url:mail': 0.40; 'some': 0.40; 'future': 0.60; 'url:3': 0.60; 'your': 0.60; 'strictly': 0.64; 'benefit': 0.66; 'course.': 0.67; 'await': 0.76; 'believe,': 0.84; 'chrisa': 0.84; 'returns.': 0.84; 'task,': 0.91; 'instant': 0.98 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 :cc:content-type; bh=HiqEu1WdNJI6iLKXy5+1wd9L77rOADelsut2kiJg0oM=; b=GJTj9EE8lJhUq0NaLZtmIyGYhOpdmFIjRuHWCpqT8Fcls8oCModGHMMf7Qj7hnaTdl Z7dYIJUcXFPtw9pC8gzrauE6o0Xziy5WfYjwUf0zquqv9ZLQD/5aTXz+zVx5CgMbqYkD RmvfnRbKdE0pMGdfIFVznfdUqYz7/+lfhNOpyQzwoq6cnbYAEfYSMrq1XgIuDfTsp55k Eo9IK1CqjBT0Zk9k5mRE8yVLqOCoCKiObJoGAQpXCxKuw8m/pggyMmuCbW9/muWkgz35 sNmPuujcNNdqkMmuzbwq4Y1XvugRqD4ghs3TxaK67XT/yWjEkxCLny6VvSQd7OzYnY38 Jh4g== 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:cc:content-type; bh=HiqEu1WdNJI6iLKXy5+1wd9L77rOADelsut2kiJg0oM=; b=K8ce7gcgaNO8XbGZVPgYfT8u+WKVXt/OdZy1H+DgcdG14BzX2CiifqrYpRA7L6vg5G l6fztfXLA5lII85XOfLYxUuKqopwz9PADxpQ4VznA9Sks23EjP8xU1/38zkPTM6jvT1O UOEyLFoEQrqDQPL3TuViqy2XpJ0tzERf4keDy5Z34WkbuA6Yty8867RP2UGaYOz+S5fN M0GvDBZG0M5tFQzep+lBLExu7AlYZU91YLNnjkVodtZvuKhDDMTH/mQkwWB9/SwfT9va FF7esf2Xy2ECkxEU8JoCSsf7E4OhK//jBbE0DaIgED3CJ/oBR3qJsCg0g1Tb0pEHdII8 kwxA== X-Gm-Message-State: AG10YORQE38aSzm0pCIiWbvG3z+3y40WYUdsGZQVOu2Nk50l4GZh6s7pqsz3pnH24YQDAadQBj+87t0IrMOyng== X-Received: by 10.60.178.70 with SMTP id cw6mr17293128oec.46.1455628966299; Tue, 16 Feb 2016 05:22:46 -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:103005 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. The coroutine you schedule with that function will not cause your current coroutine to wait unless you await the future it returns. [1] https://docs.python.org/3/library/asyncio-task.html#asyncio.ensure_future On Mon, Feb 15, 2016, 23:53 Chris Angelico wrote: > On Mon, Feb 15, 2016 at 6:39 PM, Paul Rubin > wrote: > > "Frank Millman" writes: > >> 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. > > > > Threads in Python don't get you parallelism either, of course. > > > > They can. The only limitation is that, in CPython (and some others), > no two threads can concurrently be executing Python byte-code. The > instant you drop into a C-implemented function, it can release the GIL > and let another thread start running. Obviously this happens any time > there's going to be a blocking API call (eg if one thread waits on a > socket read, others can run), but it can also happen with > computational work: > > import numpy > import threading > > def thread1(): > arr = numpy.zeros(100000000, dtype=numpy.int64) > while True: > print("1: %d" % arr[0]) > arr += 1 > arr = (arr * arr) % 142957 > > def thread2(): > arr = numpy.zeros(100000000, dtype=numpy.int64) > while True: > print("2: %d" % arr[0]) > arr += 2 > arr = (arr * arr) % 142957 > > threading.Thread(target=thread1).start() > thread2() > > This will happily keep two CPU cores occupied. Most of the work is > being done inside Numpy, which releases the GIL before doing any work. > So it's not strictly true that threading can't parallelise Python code > (and as mentioned, it depends on your interpreter - Jython can, I > believe, do true multithreading), but just that there are limitations > on what can execute concurrently. > > ChrisA > -- > https://mail.python.org/mailman/listinfo/python-list >