Skip to main content

Write classes with async def __ainit__

Project description

async-class

PyPI - License Wheel PyPI PyPI Coverage Status GitHub Workflow Status

Adding abillity to write classes with awaitable initialization function.

Example

import asyncio
from async_class import AsyncClass


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


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


asyncio.run(main())

Documentation

Module provides useful abstractions for writing async code.

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()
    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())

AsyncClass

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

import asyncio
from async_class import AsyncClass


class MyClass(AsyncClass):
    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())

AsyncClassBase

Is a base wrapper with metaclass has no additional methods and properties like self.loop and TaskStore related helpers (self.create_task, self.create_future).

import asyncio
from async_class import AsyncClassBase


class MyAsyncClass(AsyncClassBase):
    async def __ainit__(self):
        loop = asyncio.get_event_loop()
        future = loop.create_future()
        loop.call_soon(future.set_result)
        await future


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


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.2.0.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

async_class-0.2.0-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: async-class-0.2.0.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.3

File hashes

Hashes for async-class-0.2.0.tar.gz
Algorithm Hash digest
SHA256 c33efe64a83167d98da177b7578526eb1953b794fdff77c699b33cc70a1a793e
MD5 f73a21fc3274fbb74fe0d6d38c6a4467
BLAKE2b-256 83b2d3f2f8dcd73ee8e22e44d285e643216bd0ac45a5e3bd9c672b9adaae40c2

See more details on using hashes here.

File details

Details for the file async_class-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: async_class-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.3

File hashes

Hashes for async_class-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 869ab8311f02922d7e6cd9a4c713c932bd0c6d8055c5006949d10b0049782fa0
MD5 c4d72209f2ba396f513c20888f383be3
BLAKE2b-256 abd4022a3da75e5807f568a160aa3a8c03e9132294ecb9eceb898af9365df25a

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