Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #102988

Re: asyncio - run coroutine in the background

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Subject Re: asyncio - run coroutine in the background
Date Tue, 16 Feb 2016 16:51:18 +1100
Lines 45
Message-ID <mailman.154.1455601888.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>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de xKjC6lNm3wYWqFOzveTh6wGZE97IZAIw/ap/cb9uQD2g==
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
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; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 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; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'function,': 0.22; 'feb': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'mon,': 0.24; 'paul': 0.24; 'skip:t 40': 0.27; 'message- id:@mail.gmail.com': 0.27; 'executing': 0.27; 'blocking': 0.29; 'cpu': 0.29; 'gil': 0.29; 'running.': 0.29; 'thread,': 0.29; 'read,': 0.29; 'work.': 0.30; 'code': 0.30; '15,': 0.30; 'another': 0.32; "can't": 0.32; 'run': 0.33; 'class': 0.33; 'limitations': 0.33; 'that,': 0.34; 'received:google.com': 0.35; 'done': 0.35; 'but': 0.36; 'there': 0.36; 'received:209.85': 0.36; '(and': 0.36; 'depends': 0.36; 'limitation': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'being': 0.37; 'received:209.85.213': 0.37; 'release': 0.37; 'doing': 0.38; 'received:209': 0.38; 'drop': 0.38; 'subject:the': 0.39; 'some': 0.40; 'your': 0.60; 'strictly': 0.64; 'benefit': 0.66; 'course.': 0.67; 'believe,': 0.84; 'chrisa': 0.84; 'to:none': 0.91; 'instant': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=vimP1t2ap1uG1TTEJAMseN7J3WDHXZ3lc0MBlYlUdfo=; b=v9UD7CSq3yKICfsvqkG5lTpTkbHPjfg3/8LKWfCqfcsRFOZ1pWTnawk5qLH8NCZTZ+ xVGVs+cMbiJ11Lfz7TIWqhfzeBWVEwyhzRBzriojOkL0FdDvOZIKtupLM3NTXY7LXdX2 zqNEov7v2FvuS9HoSX3oR/iQnNvHWol825KbGggc4BmghiVoYJxZRLORUvILkP6CMhBA GYrTbxGMIdjn/MdMuxsQ8OcRMlYMrxvP7Odg7CyZvx2Nq8KMrGLB654iivbf4XkYRwA8 ahdIi+vxX4AeZ/CU+JB3n5KO9NCjds4csCL0Z6wdnfpLug1vyV1c55cOwSh4lUM3nNK/ J84Q==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:cc:content-type; bh=vimP1t2ap1uG1TTEJAMseN7J3WDHXZ3lc0MBlYlUdfo=; b=ARqm1APYC4kk7sH8UZ8q8k0OQkymqezwl7VtGf5elCD/X8gkUtkiLi3MO1vSDjHFPn FGgq3YMhKnfIj8j/HTDbvMYcRNGwyuW/Oof1Dkslbc3DlvglDRn2ToVIwyzCHX4h1lko 417+8ulkoY7IX4ab21yxKVAESbozj1NK80CICjOiBHMEUlHMI78lxKKb5Ry+4PYLosh3 19LL/VLr/AUQYeGCMw9Ni/DaRXYhNmY4Mypesw/634TgTZM5Jyq1NsAmY2p8sp7e0dnr +cQK0GmR0fj3aVeM8dHo9T8h+lbhJQiQgM+mqq7SDznc5oBUaW7/pwDZ+Il76Dk6o8QM 1wKw==
X-Gm-Message-State AG10YOTo82w5z2UhhyrtMQb6vP6fy0DujUow7nQw1ajysByTTCbMNfg7+rk0VEZdDS9dY98ojXVVF6Zwb9hNuQ==
X-Received by 10.50.28.105 with SMTP id a9mr17097348igh.94.1455601878743; Mon, 15 Feb 2016 21:51:18 -0800 (PST)
In-Reply-To <87h9ha8lt0.fsf@jester.gateway.pace.com>
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:102988

Show key headers only | View raw


On Mon, Feb 15, 2016 at 6:39 PM, Paul Rubin <no.email@nospam.invalid> wrote:
> "Frank Millman" <frank@chagford.com> 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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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