Skip to content

Instantly share code, notes, and snippets.

@filinvadim
Created November 9, 2017 15:10
Show Gist options
  • Save filinvadim/539f5f06b642e15245948665aa307379 to your computer and use it in GitHub Desktop.
Save filinvadim/539f5f06b642e15245948665aa307379 to your computer and use it in GitHub Desktop.
Make python object fully async
import asyncio
class _AsyncObject(object):
"""Inheriting this class allows you to define an async __init__.
So you can create objects by doing something like `await MyClass(params)`
"""
@asyncio.coroutine
def __new__(cls, *a, **kw):
instance = super().__new__(cls)
yield from instance.__init__(*a, **kw)
return instance
@asyncio.coroutine
def __init__(self):
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment