Queue¶
Added in version 0.2.1.
import asyncgui as ag
from asyncgui_ext.synctools.queue import Queue
async def producer(q):
for c in "ABC":
await q.put(c)
print('produced', c)
async def consumer(q):
async for c in q:
print('consumed', c)
q = Queue(capacity=1)
ag.start(producer(q))
ag.start(consumer(q))
produced A
produced B
consumed A
produced C
consumed B
consumed C
癖 – Quirk –¶
async def async_fn1(q, consumed):
await q.put('A')
await q.put('B')
item = await q.get()
consumed.append(item)
await q.put('C')
item = await q.get()
consumed.append(item)
async def async_fn2(q, consumed):
item = await q.get()
consumed.append(item)
consumed = []
q = Queue(capacity=1)
ag.start(async_fn1(q, consumed))
ag.start(async_fn2(q, consumed))
print(consumed)
['B', 'C', 'A']
上記の出力を見てわかるように A
, B
, C
の順でキューに入れたのに
consumed
には B
, C
, A
の順で入っています。
このような事が起こるのは asyncgui
が自身ではメインループを持たない故にタイマー機能を提供できない事に起因します。
なので外部のタイマー機能を利用する事でこの問題を解消する選択肢を用意する予定なのですが、それまではこういうものだと諦めてください。
因みに Kivy
を使っているのであれば Kivy
のタイマー機能を用いる事でこの問題を解決済みの asynckivy-ext-queue
というモジュールが既にあるので氣になる人はそちらをご利用ください。
- exception Closed¶
Occurs when:
one tries to get an item from a queue that is in the
CLOSED
state.one tries to get an item from an empty queue that is in the
HALF_CLOSED
state.one tries to put an item into a queue that is in the
CLOSED
orHALF_CLOSED
state.
- class Queue(*, capacity: int | None = None, order: Literal['fifo', 'lifo', 'small-first'] = 'fifo')¶
- Parameters:
capacity – Cannot be zero. Unlimited if None.
- close()¶
Fully closes the queue. Putting an item into it is no longer allowed. Getting an item from it is no longer allowed. All the items it holds will be discarded.
- half_close()¶
Partially closes the queue. Putting an item into it is no longer allowed.
- put_nowait(item)¶
queue.put_nowait(item)
- exception QueueException¶
Base class of all the queue-related exceptions.
- class QueueState(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)¶
Enum class that represents the state of the Queue.
- CLOSED¶
Putting an item into the queue is not allowed. Getting an item from the queue is not allowed.
- HALF_CLOSED¶
Putting an item into the queue is not allowed.
- OPENED¶
All operations are allowed.
- exception WouldBlock¶
Raised by X_nowait functions if X would block.