Write classes with async def __ainit__
Project description
async-class
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
Built Distribution
File details
Details for the file async-class-0.1.0.tar.gz
.
File metadata
- Download URL: async-class-0.1.0.tar.gz
- Upload date:
- Size: 3.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7a00bfbe212cb4776055cef56571e62dd7443164d9ea179edf1ea3e9befddcae |
|
MD5 | 72f0bb04a2a59cecbea988291a9ad0d7 |
|
BLAKE2b-256 | c0f8a15490ffdd0fb6f4e41633f7c55fbeaa47efa1dc355541b3f27ab7e3bfdd |
File details
Details for the file async_class-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: async_class-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.1 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/46.0.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1da81420beba6bbbd9961030c13609c03413f271c46a2da2806b043bc2b791d5 |
|
MD5 | de94e015d77e64cbc08a33c68957090d |
|
BLAKE2b-256 | deb5c1d6787caa12f287eebef13b5de40252ebb7abc9fc50920093fd1d75e30d |