How to set class attribute with await in __init__ How to set class attribute with await in __init__ python-3.x python-3.x

How to set class attribute with await in __init__


Most magic methods aren't designed to work with async def/await - in general, you should only be using await inside the dedicated asynchronous magic methods - __aiter__, __anext__, __aenter__, and __aexit__. Using it inside other magic methods either won't work at all, as is the case with __init__ (unless you use some tricks described in other answers here), or will force you to always use whatever triggers the magic method call in an asynchronous context.

Existing asyncio libraries tend to deal with this in one of two ways: First, I've seen the factory pattern used (asyncio-redis, for example):

import asynciodsn = "..."class Foo(object):    @classmethod    async def create(cls, settings):        self = Foo()        self.settings = settings        self.pool = await create_pool(dsn)        return selfasync def main(settings):    settings = "..."    foo = await Foo.create(settings)

Other libraries use a top-level coroutine function that creates the object, rather than a factory method:

import asynciodsn = "..."async def create_foo(settings):    foo = Foo(settings)    await foo._init()    return fooclass Foo(object):    def __init__(self, settings):        self.settings = settings    async def _init(self):        self.pool = await create_pool(dsn)async def main():    settings = "..."    foo = await create_foo(settings)

The create_pool function from aiopg that you want to call in __init__ is actually using this exact pattern.

This at least addresses the __init__ issue. I haven't seen class variables that make asynchronous calls in the wild that I can recall, so I don't know that any well-established patterns have emerged.


Another way to do this, for funsies:

class aobject(object):    """Inheriting this class allows you to define an async __init__.    So you can create objects by doing something like `await MyClass(params)`    """    async def __new__(cls, *a, **kw):        instance = super().__new__(cls)        await instance.__init__(*a, **kw)        return instance    async def __init__(self):        pass#With non async super classesclass A:    def __init__(self):        self.a = 1class B(A):    def __init__(self):        self.b = 2        super().__init__()class C(B, aobject):    async def __init__(self):        super().__init__()        self.c=3#With async super classesclass D(aobject):    async def __init__(self, a):        self.a = aclass E(D):    async def __init__(self):        self.b = 2        await super().__init__(1)# Overriding __new__class F(aobject):    async def __new__(cls):        print(cls)        return await super().__new__(cls)    async def __init__(self):        await asyncio.sleep(1)        self.f = 6async def main():    e = await E()    print(e.b) # 2    print(e.a) # 1    c = await C()    print(c.a) # 1    print(c.b) # 2    print(c.c) # 3    f = await F() # Prints F class    print(f.f) # 6import asyncioloop = asyncio.get_event_loop()loop.run_until_complete(main())


I would recommend a separate factory method. It's safe and straightforward. However, if you insist on a async version of __init__(), here's an example:

def asyncinit(cls):    __new__ = cls.__new__    async def init(obj, *arg, **kwarg):        await obj.__init__(*arg, **kwarg)        return obj    def new(cls, *arg, **kwarg):        obj = __new__(cls, *arg, **kwarg)        coro = init(obj, *arg, **kwarg)        #coro.__init__ = lambda *_1, **_2: None        return coro    cls.__new__ = new    return cls

Usage:

@asyncinitclass Foo(object):    def __new__(cls):        '''Do nothing. Just for test purpose.'''        print(cls)        return super().__new__(cls)    async def __init__(self):        self.initialized = True

async def f():    print((await Foo()).initialized)loop = asyncio.get_event_loop()loop.run_until_complete(f())

Output:

<class '__main__.Foo'>True

Explanation:

Your class construction must return a coroutine object instead of its own instance.