How can I define a class with
await in the constructor or class body?
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
await - in general, you should only be using
await inside the dedicated asynchronous magic methods -
__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.
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 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)
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.
This is the first book on synthetic data for deep learning, and its extensive coverage could make this book the standard benchmark for synthetic data for years to come. The book can also serve as an i...
The ability to identify patterns is an essential component of sensory intelli- gent machines. Pattern recognition is therefore an indispensible component of the so-called “Intelligent Control System...
Systems programming provides the basis for global calculation. Developing performance-sensitive code requires a programming language that allows programmers to control the use of memory, processor tim...
A Problem-Solver’s Guide to Building Real-World Intelligent Systems. Data is the new oil and Machine Learning is a powerful concept and framework for making the best out of it. In this age of aut...