Skip to main content

Overview

QueueChannel puts every post on a queue as well as in an in-memory history. Agents read messages() as they would any channel. A consumer, usually another thread, calls wait() or iterates the channel to receive each message the moment it is posted. Use it to drive a UI, a log, a websocket or any other live view of a run without polling.

Import

Constructor

queue.Queue | multiprocessing.Queue | Any
default:"queue.Queue()"
The queue each post is put on. Anything with put, get and get_nowait works. Defaults to a new queue.Queue.

Attributes

Methods

post()

Record one message in the history and put it on the queue.
The message put on the queue is the same dictionary messages() returns: {"agent_name": ..., "content": ...}. content is converted with str().

messages()

Return a copy of every message posted through this instance, oldest first. It reads the history, not the queue, so it is unaffected by consumers taking messages off the queue.

wait()

Block until the next message is on the queue, and take it.
Parameters:
  • timeout (float, optional): Seconds to wait. None waits forever.
Returns: The next message, or None when the channel was closed or the timeout passed.

Iteration

for message in channel calls wait() with no timeout until it returns None, so the loop ends when the producer calls close().

close()

Put a None sentinel on the queue. This wakes the waiting consumer and ends its iteration. It does not close the queue, and later posts still go on it.

clear()

Empty the history and drain every message waiting on the queue.

Usage Examples

Wait with a timeout

Share the queue across processes

Pass a multiprocessing.Queue to receive posts in another process. Only the queue is shared: each process’s messages() holds only what that process posted.
More examples are in the QueueChannel quickstart.

Notes

  • The history is in memory. Nothing is written to disk. When the process exits, the conversation is gone. Use FileChannel or SqliteChannel to keep it.
  • One consumer per sentinel. close() puts one None on the queue, which ends one consumer’s loop. With several consumers, call close() once for each.
  • Every message is delivered once. A message taken by one consumer is gone from the queue. To give several consumers every message, fan it out yourself from a single consumer.

Source Code

View the source on GitHub.