Skip to main content

Write classes with async def __ainit__

Project description

Coveralls License Build status Wheel Latest version

async-class

Adding abillity to write classes with awaitable initialization function.

Usage example

import asyncio
from async_class import AsyncClass, AsyncObject, task, link


class MyAsyncClass(AsyncClass):
     async def __ainit__(self):
          # Do async staff here
          pass


class MainClass(AsyncObject):
     async def __ainit__(self):
          # Do async staff here
          pass

     async def __adel__(self):
          """ This method will be called when object will be closed """
          pass


class RelatedClass(AsyncObject):
     async def __ainit__(self, parent: MainClass):
          link(self, parent)


async def main():
     instance = await MyAsyncClass()
     print(instance)

     main_instance = await MainClass()
     related_instance = await RelatedClass(main_instance)

     assert not main_instance.is_closed
     assert not related_instance.is_closed

     await main_instance.close()
     assert main_instance.is_closed

     # will be closed because linked to closed main_instance
     assert related_instance.is_closed

asyncio.run(main())

Documentation

Async objects might be created when no one event loop has been running. self.loop property is lazily evaluated.

Module provides useful abstractions for writing async code.

Objects inherited from AsyncClass might have their own __init__ method, but it strictly not recommend.

Class AsyncClass

Is a base wrapper with metaclass has no TaskStore instance and additional methods like self.create_task and self.create_future.

This class just solves the initialization problem:

import asyncio
from async_class import AsyncClass


class MyAsyncClass(AsyncClass):
   async def __ainit__(self):
       future = self.loop.create_future()
       self.loop.call_soon(future.set_result, True)
       await future


async def main():
   instance = await MyAsyncClass()
   print(instance)

asyncio.run(main())

Class AsyncObject

Base class with task store instance and helpers for simple task management.

import asyncio
from async_class import AsyncObject


class MyClass(AsyncObject):
   async def __ainit__(self):
       self.task = self.create_task(asyncio.sleep(3600))


async def main():
   obj = await MyClass()

   assert not obj.task.done()

   await obj.close()

   assert obj.task.done()


asyncio.run(main())

Class TaskStore

TaskStore is a task management helper. One instance has create_task() and create_future() methods and all created entities will be destroyed when TaskStore will be closed via close() method.

Also, a task store might create a linked copy of the self, which will be closed when the parent instance will be closed.

import asyncio
from async_class import TaskStore


async def main():
   store = TaskStore(asyncio.get_event_loop())

   task1 = store.create_task(asyncio.sleep(3600))

   child_store = store.get_child()
   task2 = child_store.create_task(asyncio.sleep(3600))

   await store.close()

   assert task1.done() and task2.done()


asyncio.run(main())

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

async-class-0.5.0.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

async_class-0.5.0-py2-none-any.whl (5.3 kB view details)

Uploaded Python 2

File details

Details for the file async-class-0.5.0.tar.gz.

File metadata

  • Download URL: async-class-0.5.0.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for async-class-0.5.0.tar.gz
Algorithm Hash digest
SHA256 e2c02a67ea40b606bc281b8cac016c0b194aa7f48274c43bf5cbe385bcdaf39c
MD5 d6f8e17118fed35aa362e20a46251aef
BLAKE2b-256 8843cdae41799d82b0d766f784c2b14d08d53f9739aecc481aeaea5394c3232b

See more details on using hashes here.

File details

Details for the file async_class-0.5.0-py2-none-any.whl.

File metadata

  • Download URL: async_class-0.5.0-py2-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for async_class-0.5.0-py2-none-any.whl
Algorithm Hash digest
SHA256 b11390fcf465fd07c2247668f1698e2e4699be2c4d18991413705efef21fadc7
MD5 6a452f622890b9a3414bc73765ceed59
BLAKE2b-256 d57f5882dbcc97deb53de472551dcdf930438fe1ae78961a99704c53cc565c2d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page