1

I am trying to write objects that then will be used doing asyncio tasks. The constructor of the class is as follow:

def __init__(self, **kwargs):
    for a,b in kwargs.items():
        setattr(self, a, b)  #dynamically sets values to parent class props

My question is: When working with classes in async, is it needed to make everything in the class awaitable ?, doing such would need to write a lot more code since it is almost 1 await per line when accessing class props. If the function can execute relatively fast (very light calculations like above) can I leave them sync ?

Paaksing
  • 432
  • 1
  • 4
  • 17
  • 1
    No, that is not necessary. – Klaus D. Aug 14 '20 at 19:12
  • @KlausD. Can you explain why it is not necessary ?, because it is fully CPU bound ? – Paaksing Aug 14 '20 at 19:13
  • Imagine all code had to be async. You couldn't use more than 90% of the Python standard library with asyncio. – Klaus D. Aug 14 '20 at 19:14
  • 1
    It isn’t possible to make `__init__` awaitable. Using `async def` makes a function return a coroutine and `__init__` must always return `None`. – dirn Aug 14 '20 at 19:36
  • 1
    @dirn Technically, it *is* possible to make `__init__` awaitable. [See this question](https://stackoverflow.com/questions/33128325/how-to-set-class-attribute-with-await-in-init) for some ways to do it. Still, it's probably not a good idea, even if it is possible. – dano Aug 14 '20 at 20:11

0 Answers0