简体   繁体   English

如何在 __init__ 中使用 await 设置类属性

[英]How to set class attribute with await in __init__

How can I define a class with await in the constructor or class body?如何在构造函数或类主体中使用await定义一个类?

For example what I want:例如我想要的:

import asyncio

# some code


class Foo(object):

    async def __init__(self, settings):
        self.settings = settings
        self.pool = await create_pool(dsn)

foo = Foo(settings)
# it raises:
# TypeError: __init__() should return None, not 'coroutine'

or example with class body attribute:或具有类主体属性的示例:

class Foo(object):

    self.pool = await create_pool(dsn)  # Sure it raises syntax Error

    def __init__(self, settings):
        self.settings = settings

foo = Foo(settings)

My solution (But I would like to see a more elegant way)我的解决方案(但我希望看到更优雅的方式)

class Foo(object):

    def __init__(self, settings):
        self.settings = settings

    async def init(self):
        self.pool = await create_pool(dsn)

foo = Foo(settings)
await foo.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__ .大多数魔术方法并非设计为与async def / await一起使用 - 通常,您应该只在专用异步魔术方法中使用await - __aiter____anext____aenter____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.在其他魔术方法中使用它要么根本不起作用,就像__init__的情况一样(除非您使用此处其他答案中描述的一些技巧),或者会迫使您始终使用在异步上下文中触发魔术方法调用的任何东西.

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):现有asyncio库倾向于以两种方式之一来处理这个问题:首先,我看到了使用的工厂模式(例如asyncio-redis ):

import asyncio

dsn = "..."

class Foo(object):
    @classmethod
    async def create(cls, settings):
        self = Foo()
        self.settings = settings
        self.pool = await create_pool(dsn)
        return self

async 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 asyncio

dsn = "..."

async def create_foo(settings):
    foo = Foo(settings)
    await foo._init()
    return foo

class 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.您要在__init__中调用的来自aiopgcreate_pool函数实际上正在使用这种精确模式。

This at least addresses the __init__ issue.这至少解决了__init__问题。 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 classes

class A:
    def __init__(self):
        self.a = 1

class 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 classes

class D(aobject):
    async def __init__(self, a):
        self.a = a

class 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 = 6

async 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) # 6

import asyncio
loop = 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:但是,如果您坚持使用__init__()async版本,这里有一个示例:

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:用法:

@asyncinit
class 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.你的类构造必须返回一个coroutine对象而不是它自己的实例。

Better yet you can do something like this, which is very easy:更好的是你可以做这样的事情,这很容易:

import asyncio

class Foo:
    def __init__(self, settings):
        self.settings = settings

    async def async_init(self):
        await create_pool(dsn)

    def __await__(self):
        return self.async_init().__await__()

loop = asyncio.get_event_loop()
foo = loop.run_until_complete(Foo(settings))

Basically what happens here is __init__() gets called first as usual.基本上这里发生的是__init__()像往常一样首先被调用。 Then __await__() gets called which then awaits async_init() .然后__await__()被调用,然后等待async_init()

[Almost] canonical answer by @ojii [几乎] @ojii 的规范回答

@dataclass
class Foo:
    settings: Settings
    pool: Pool

    @classmethod
    async def create(cls, settings: Settings, dsn):
        return cls(settings, await create_pool(dsn))

If you're using Python3.7 or newer then you can use asyncio.run : 如果您使用的是Python3.7或更高版本,则可以使用asyncio.run

import asyncio


# some code


class Foo(object):

    async def __init(self):
        self.pool = await create_pool(dsn)

    def __init__(self, settings):
        self.settings = settings
        asyncio.run(self.__init)


foo = Foo(settings)

Note, this won't work if you're instantiating Foo in an asynchronous function that's already running. 请注意,如果要在已经运行的异步函数中实例化Foo ,则此方法将无效。 See this blog post for a discussion on how to handle this scenario and a nice discussion of asynchronous programming in Python. 请参阅此博客文章 ,以获取有关如何处理这种情况的讨论,以及有关Python中异步编程的精彩讨论。

The AsyncObj class with __ainit__ "async-constructor":带有__ainit__ "async-constructor" 的 AsyncObj 类:

class AsyncObj:
    def __init__(self, *args, **kwargs):
        """
        Standard constructor used for arguments pass
        Do not override. Use __ainit__ instead
        """
        self.__storedargs = args, kwargs
        self.async_initialized = False

    async def __ainit__(self, *args, **kwargs):
        """ Async constructor, you should implement this """

    async def __initobj(self):
        """ Crutch used for __await__ after spawning """
        assert not self.async_initialized
        self.async_initialized = True
        await self.__ainit__(*self.__storedargs[0], **self.__storedargs[1])  # pass the parameters to __ainit__ that passed to __init__
        return self

    def __await__(self):
        return self.__initobj().__await__()

    def __init_subclass__(cls, **kwargs):
        assert asyncio.iscoroutinefunction(cls.__ainit__)  # __ainit__ must be async

    @property
    def async_state(self):
        if not self.async_initialized:
            return "[initialization pending]"
        return "[initialization done and successful]"

Here is example of "async class":这是“异步类”的示例:

class MyAsyncObject(AsyncObj):
    async def __ainit__(self, param1, param2=0):
        print("hello!", param1, param2)
        # go something async, e.g. go to db
    

Usage:用法:

async def example():
    my_obj = await MyAsyncObject("test", 123)

I would like to show a much easier way of initiating coroutine based method within the __init__ method.我想展示一种在__init__方法中启动基于协程的方法的更简单的方法。

import asyncio


class Foo(object):

    def __init__(self, settings):
        self.settings = settings
        loop = asyncio.get_event_loop() 
        self.pool = loop.run_until_complete(create_pool(dsn))

foo = Foo(settings)

Important point to be noted is:需要注意的重要一点是:

  • This makes the async code work as sync(blocking)这使得异步代码作为同步(阻塞)工作
  • This is not the best way to run async code, but when it comes to only initiation via a sync method eg: __init__ it will be a good fit.这不是运行异步代码的最佳方式,但是当它仅通过同步方法启动时,例如: __init__它将是一个很好的选择。
  • After initiation, you can run the async methods from the object with await.启动后,您可以使用 await 从对象运行异步方法。 ie await foo.pool.get(value)await foo.pool.get(value)
  • Do not try to initiate via an await call you will get RuntimeError: This event loop is already running不要尝试通过await调用启动,你会得到RuntimeError: This event loop is already running

Vishnu shettigar's answer is so far the simplest, except that his async_init method doesn't return the object itself so foo isn't assigned a Foo instance.到目前为止,Vishnu shettigar 的答案是最简单的,除了他的async_init方法不返回对象本身,因此没有为foo分配Foo实例。 As for OP's purpose, the most elegant way to construct the class IMHO is至于OP的目的,构造类恕我直言的最优雅的方式是

import asyncio

class Foo:
    def __init__(self, settings):
        self.settings = settings

    def __await__(self):
        self.pool = asyncio.create_task(create_pool(dsn))
        yield from self.pool
        self.pool = self.pool.result()
        return self

To initialize the object, do the following要初始化对象,请执行以下操作

def main():
    loop = asyncio.get_event_loop()
    foo = loop.run_until_complete(Foo(settings))

Or或者

async def main():
    foo = await Foo(settings)

we could convert the async call to sync call by running the async code manually through asyncio.run()我们可以通过asyncio.run()手动运行异步代码,将异步调用转换为同步调用

class Foo:
    async def __ainit__(self, param):
        self._member = await some_async_func(param)

    def __init__(self, param):
        asyncio.run(self.__ainit__(param))

Depending on your needs, you can also use AwaitLoader from: https://pypi.org/project/async-property/根据您的需要,您还可以使用来自以下网址的AwaitLoaderhttps ://pypi.org/project/async-property/

From the docs:从文档:

AwaitLoader will call await instance.load() , if it exists, before loading properties. AwaitLoader将在加载属性之前调用 await instance.load() (如果存在)。

This worked for me in Python 3.9这在 Python 3.9 中对我有用


from aiobotocore.session import AioSession
import asyncio




class SomeClass():

    def __init__(self):
        asyncio.run(self.async_init())
        print(self.s3)

    async def async_init(self):
        self.s3 = await AioSession().create_client('s3').__aenter__()

Everyone can try: https://pypi.org/project/asyncinit/大家可以试试: https://pypi.org/project/asyncinit/

  • pip install asyncinit点安装异步初始化
from asyncinit import asyncinit

@asyncinit
class MyClass:
    async def __init__(self, param):
        self.val = await self.deferredFn(param)

    async def deferredFn(self, x):
        # ...
        return x + 2

obj = await MyClass(42)
assert obj.val == 44

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM