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


Groups > comp.lang.python > #103265

Re: asyncio - run coroutine in the background

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Kevin Conway <kevinjacobconway@gmail.com>
Newsgroups comp.lang.python
Subject Re: asyncio - run coroutine in the background
Date Sat, 20 Feb 2016 13:52:02 +0000
Lines 73
Message-ID <mailman.28.1455976334.13884.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> <CAPTjJmoWUDfcOe3OCaMK7=zDtW3QzS+7hqUG6DoYZ2AbqzJBUw@mail.gmail.com> <mailman.166.1455628969.22075.python-list@python.org> <87d1rwpwo2.fsf@elektro.pacujo.net> <n9vc4j$n89$1@ger.gmane.org> <CAKF=+dhXZ=yax8STAWr_gjX3Tg8yUjPRJG-7yM2_bRV2Kxm3Jg@mail.gmail.com> <mailman.173.1455636082.22075.python-list@python.org> <87r3gc608i.fsf@elektro.pacujo.net> <871t877ru6.fsf@jester.gateway.pace.com> <87oabbpzoo.fsf@elektro.pacujo.net> <87twl36amj.fsf@jester.gateway.pace.com> <87egc7pw6t.fsf@elektro.pacujo.net>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de 8vwIGXXW0YKzQ3A+UBcACAxKLcQpwP/DOQ7pveIRgWcw==
Return-Path <kevinjacobconway@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'defaults': 0.05; 'source.': 0.05; 'override': 0.07; 'socket': 0.07; 'twisted,': 0.07; 'answering': 0.09; 'caveat': 0.09; 'mutable': 0.09; 'sake': 0.09; 'subclass': 0.09; 'threads.': 0.09; 'url:github': 0.09; 'received:209.85.218': 0.10; 'thread': 0.10; 'python': 0.10; 'assume': 0.11; 'commit': 0.15; '[2].': 0.16; '[3],': 0.16; '[5]': 0.16; 'async': 0.16; 'dislike': 0.16; 'erlang.': 0.16; 'executor': 0.16; 'getaddrinfo': 0.16; 'ipv6': 0.16; 'letters.': 0.16; 'maintainers': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'stuff.': 0.16; 'subject:run': 0.16; 'thankfully,': 0.16; 'threads': 0.16; 'twisted': 0.16; 'version?': 0.16; 'wrote:': 0.16; 'looked': 0.16; '&gt;': 0.18; 'library': 0.20; 'to:2**1': 0.21; "aren't": 0.22; 'default,': 0.22; 'implicit': 0.22; 'rid': 0.22; 'programming': 0.22; 'appears': 0.23; 'feb': 0.23; 'bigger': 0.23; 'sat,': 0.23; 'this:': 0.23; 'unlike': 0.23; "haven't": 0.24; 'written': 0.24; 'header:In-Reply-To:1': 0.24; 'paul': 0.24; 'sort': 0.25; "i've": 0.25; 'installed': 0.26; 'least': 0.27; 'question': 0.27; 'message-id:@mail.gmail.com': 0.27; '[2]': 0.27; 'cancel': 0.27; "skip:' 10": 0.28; 'about.': 0.29; 'blocking': 0.29; 'methods.': 0.29; 'objects': 0.29; 'allows': 0.30; 'connection': 0.30; 'url:mailman': 0.30; "we're": 0.30; 'creating': 0.30; 'code': 0.30; 'connections': 0.30; "i'd": 0.31; '[1]': 0.32; 'addresses': 0.32; 'though,': 0.32; 'maybe': 0.33; 'url:python': 0.33; 'legacy': 0.33; 'point,': 0.33; 'url:listinfo': 0.34; 'handle': 0.34; 'received:google.com': 0.35; 'trouble': 0.35; 'protocol': 0.35; 'something': 0.35; 'remote': 0.35; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'url:org': 0.36; 'received:209.85': 0.36; 'possible': 0.36; 'cases': 0.36; 'to:addr :python-list': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'method': 0.37; 'client': 0.37; 'version': 0.38; 'received:209': 0.38; 'names': 0.38; 'skip:p 20': 0.38; 'does': 0.39; 'subject:the': 0.39; 'url:mail': 0.40; 'to:addr:python.org': 0.40; 'where': 0.40; 'called': 0.40; 'your': 0.60; 'skip:u 10': 0.61; 'default': 0.61; 'programs': 0.62; '20,': 0.66; 'accessed': 0.66; 'inherit': 0.66; 'tasks.': 0.66; 'virtually': 0.66; 'stand': 0.67; 'choose': 0.68; 'gotten': 0.76; 'concept.': 0.84; 'each,': 0.84; 'history,': 0.84; 'leverages': 0.84; "op's": 0.84; 'pain': 0.84; 'url:cpython': 0.84; 'url:master': 0.84; 'careful': 0.91; 'notorious': 0.91; 'url:internet': 0.97
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=qAFlgqPUe7z/uBbQPXgJGQDqm81mYemQI2ye9koV118=; b=fCL2BDrTVTIj3T4K9c6eDzHF9AMJ7ueuIR005X6/qgH0PcDgzoaMLT2Ie08TTY7iLx OOrz4AzkQ+Ok/cTMMre8tL6SUgyHSl8KyNV1cnf8/Jlg3nGKBk/YySh10I3KlpyNJLMx LANoFXb9x+MPlp4GzV6kZMqNuWCxvjf6OOA8xw3gHsi0BNfHMxcNojQgIxndl4iNBIzu 0713dKAV8ogKmOnp2lwxc05CF6+3cUxq0IdIuww7T8LYQplpQmaGzBrn2pTqsH/TWUMV 9NWYaxz0ScBbWGrm86xsOKlgu1+fGe53cmdTBb6xQ4mAkHoKTxz9UWaWgwzL9KQ4+UEq T2yw==
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=qAFlgqPUe7z/uBbQPXgJGQDqm81mYemQI2ye9koV118=; b=HXrr6vbqNrH7dpOofzgHiJe8nmKlF9BJ5A9e0WX1ZfJKK8rS1DjNq5xbjpLNmzjC+f 2wqqXPw+FwgnZZjSI+OrR9zK8i4TwPpQ/55oEYa1NEKPxxbAkPA08laMoPf0x4iOset2 HSZZ3f9vjMjAQ7u2/NE/zPkhZG8GS5/I4AU8fTYmkYEJ/f3nePGVhOMILBszVFXabQCu /u3tRhU1EHGsSin290/Vv31YwTtNw81QVfhTNpu1SI/zhxqqR4EHu4/YTByZDvIyilLZ pxt2UsqrbMhHOIs4XJTM104LzxYqMuTfzEcgEio0mi/hqt5ZyGB/hNdsns4k5e0Y2zeN sQzQ==
X-Gm-Message-State AG10YOTr+WNGSVCKHcx0LA4Zw7mEMPw3INfjOXpqd0TWKrekgbBbccxB6cK6+iMgTd6vw12ATlnupl1vx8JzIg==
X-Received by 10.202.71.87 with SMTP id u84mr15576830oia.75.1455976332043; Sat, 20 Feb 2016 05:52:12 -0800 (PST)
In-Reply-To <87egc7pw6t.fsf@elektro.pacujo.net>
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 <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:103265

Show key headers only | View raw


> getaddrinfo is a notorious pain but I think it's just a library issue; an
async version should be possible in principle.  How does Twisted handle
it?  Does it have a version?

I think we're a little outside the scope of OP's question at this point,
but for the sake of answering this:

There are a few cases that I know of where Twisted uses the standard lib
socket DNS methods. One is when resolving names to IPv6 addresses [1] when
creating a client connection to a remote source. The other is in the
default DNS resolver that is installed in the reactor [2]. Creating client
connections allows the call to 'getaddrinfo' to block without mitigation.
The default DNS resolver, unfortunately, dispatches calls of
'gethostbyname' to a thread pool.

Without seeing the commit history, I'd assume the use of 'socket' and
threads by default is an artifact that predates the implementation of the
DNS protocol in Twisted. Twisted has, in 'twisted.names' [3], a DNS
protocol that uses UDP and leverages the reactor appropriately. Thankfully,
Twisted has a reactor method called 'installResolver' [4] that allows you
to hook in any DNS resolver implementation you want so you aren't stuck
using the default, threaded implementation.

As far as asyncio, it also defaults to an implementation that delegates to
an executor (default: threadpool). Unlike Twisted, though, it appears to
require a subclass of the event loop to override the 'getaddrinfo' method
[5].

[1]
https://github.com/twisted/twisted/blob/trunk/twisted/internet/tcp.py#L622
[2]
https://github.com/twisted/twisted/blob/trunk/twisted/internet/base.py#L257
[3] https://github.com/twisted/twisted/tree/trunk/twisted/names
[4]
https://github.com/twisted/twisted/blob/trunk/twisted/internet/base.py#L509
[5]
https://github.com/python/cpython/blob/master/Lib/asyncio/base_events.py#L572

On Sat, Feb 20, 2016, 03:31 Marko Rauhamaa <marko@pacujo.net> wrote:

> Paul Rubin <no.email@nospam.invalid>:
>
> > I've just felt depressed whenever I've looked at any Python async
> > stuff. I've written many Python programs with threads and not gotten
> > into the trouble that people keep warning about.
>
> Programming-model-wise, asyncio is virtually identical with threads. In
> each, I dislike the implicit state concept. I want the state to stand
> out with big block letters.
>
> > I've just felt depressed whenever I've looked at any Python async
> > stuff. I've written many Python programs with threads and not gotten
> > into the trouble that people keep warning about. But I haven't really
> > understood the warnings, so maybe they know something I don't. I just
> > write in a multiprocessing style, with every mutable object owned by
> > exactly one thread and accessed only by RPC through queues, sort of a
> > poor man's Erlang. There's a performance hit but there's a much bigger
> > one from using Python in the first place, so I just live with it.
>
> Good for you if you have been able to choose your own programming model.
> Most people have to deal with a legacy mess. Also, maintainers who
> inherit your tidy code might not be careful to ship only nonmutable
> objects in the queues.
>
> Your way of using threads works, of course, with the caveat that it is
> not possible to get rid of a blocking thread from the outside. With
> asyncio, you can at least cancel tasks.
>
>
> Marko
> --
> https://mail.python.org/mailman/listinfo/python-list
>

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