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


Groups > comp.lang.python > #102140

Re: Question about asyncio and blocking operations

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Alberto Berti <alberto@metapensiero.it>
Newsgroups comp.lang.python
Subject Re: Question about asyncio and blocking operations
Date Wed, 27 Jan 2016 00:57:10 +0100
Lines 46
Message-ID <mailman.25.1453852646.2338.python-list@python.org> (permalink)
References <n8038j$575$1@ger.gmane.org>
Mime-Version 1.0
Content-Type text/plain
X-Trace news.uni-berlin.de 2gCYyNrABdOqNYFSJiHhHgr2KlEiQ774pFB0hQpfi9MQ==
Cancel-Lock sha1:PfbAtxJeeKUQKZ/bfcPrVCFiGB8=
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'initialize': 0.05; 'method.': 0.05; 'subject:Question': 0.05; '__init__': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.13; ':-).': 0.16; 'async': 0.16; 'coroutines': 0.16; 'executed.': 0.16; 'flow,': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'semantically': 0.16; 'simpler,': 0.16; 'tran': 0.16; 'this:': 0.23; 'import': 0.24; 'header:User-Agent:1': 0.26; 'header:X -Complaints-To:1': 0.26; 'correct': 0.28; 'function': 0.28; 'context,': 0.29; 'classes': 0.30; 'transaction': 0.30; 'code': 0.30; 'another': 0.32; 'skip:_ 10': 0.32; 'non': 0.32; 'maybe': 0.33; 'point': 0.33; 'class': 0.33; 'maintaining': 0.34; 'clear': 0.35; 'instance': 0.35; 'problem.': 0.35; 'should': 0.36; 'cases': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'method': 0.37; 'turn': 0.37; 'received:org': 0.37; 'stuff': 0.38; 'means': 0.39; 'data': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'some': 0.40; 'your': 0.60; 'hope': 0.61; 'skip:u 10': 0.61; 'management': 0.64; 'developed': 0.66; '>>>>>': 0.66; 'guaranteed': 0.67; 'frank': 0.72; 'await': 0.76; 'alberto': 0.84
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host h145-ipv4-31-44-165.mynet.it
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux)
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
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:102140

Show key headers only | View raw


>>>>> "Frank" == Frank Millman <frank@chagford.com> writes:

    Frank> Now I have another problem. I have some classes which retrieve some
    Frank> data from the database during their __init__() method. I find that it
    Frank> is not allowed to call a coroutine from __init__(), and it is not
    Frank> allowed to turn __init__() into a coroutine.

IMHO this is semantically correct for a method tha should really
initialize that instance an await in the __init__ means having a
suspension point that makes the initialization
somewhat... unpredictable :-).

To cover the cases when you need to call a coroutine from a non
coroutine function like __init__ I have developed a small package that
helps maintaining your code almost clean, where you can be sure that
after some point in your code flow, the coroutines scheduled by the
normal function have been executed. With that you can write code like
this:

    from metapensiero.asyncio import transaction

    class MyObject():

        def __init__(self):
            tran = transaction.get()
            tran.add(get_db_object('company'), cback=self._init) # get_db_object is a coroutine

        def _init(self, fut):
            self.company = fut.result()

    async external_coro(): # this is the calling context, which is a coro
        async with transction.begin():
            o = MyObject
            # maybe other stuff

        # start using your db object
        o.company...

This way the management of the "inner" coroutine is simpler, and from
your code it's clear it suspends to wait and after that all the
"stashed" coroutines are guaranteed to be executed.

Hope it helps,

Alberto

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


Thread

Re: Question about asyncio and blocking operations Alberto Berti <alberto@metapensiero.it> - 2016-01-27 00:57 +0100

csiph-web