cassiacommon

Provides commonly used utility classes and functions.

SimpleFuture

A lightweight asynchronous result container used to wait for an event to complete and retrieve its result in asynchronous code.

Example

 1import asyncio
 2import cassiacommon
 3
 4
 5async def main():
 6    print("start simple future")
 7    fut = cassiacommon.SimpleFuture()
 8
 9    async def producer():
10        print("producer sleep 3 seconds...")
11        await asyncio.sleep(3)
12        fut.set_result(200, "OK")
13        print("producer ok")
14
15    asyncio.create_task(producer())
16    code, value = await fut.wait()
17    print("simple future done:", code, value)
18
19
20asyncio.run(main())

API

class SimpleFuture

A lightweight, one-time event synchronization primitive implemented using asyncio.ThreadSafeFlag.

__init__() SimpleFuture

Initialize the object

set_result(code, value)

Sets the result and wakes up any waiters. Can only take effect once; subsequent calls are ignored.

Parameters:
  • code: Custom status code

  • value: Data to be passed

Returns:

None

async wait() Tuple[code, value]

Suspends execution until the result is ready, then returns the packaged data.

Parameters:

None

Returns:

  • code: Custom status code

  • value: Data to be passed


AsyncQueue

A lightweight asynchronous queue used to pass data between asynchronous tasks.

Example

 1import asyncio
 2import cassiacommon
 3
 4
 5async def main():
 6    print("start async queue")
 7
 8    q = cassiacommon.AsyncQueue(max_size=4)
 9    q.put_nowait("a")
10    q.put_nowait("b")
11    print(await q.get())
12    print(await q.get())
13
14    print("async queue done")
15
16
17asyncio.run(main())

API

class AsyncQueue

A lightweight, one-time event synchronization primitive implemented using asyncio.ThreadSafeFlag.

__init__(max_size=16) AsyncQueue

Initialize the object

put_nowait(item)

Puts an element into the queue without blocking. If the queue is full, the element is discarded without raising an exception.

Parameters:
  • item: Object to be enqueued

Returns:

None

async get() Any

Retrieves the element at the front of the queue. If the queue is empty, suspends execution until data becomes available.

Parameters:

None

Returns:

The earliest enqueued element