Skip to content

Merging¤

Merge messages coming from multiple receivers into a single receiver.

Usage¤

If you just need to receive the same type of messages but from multiple sources in one stream, you can use merge() to create a new receiver that will receive messages from all the given receivers:

async for message in merge(receiver1, receiver2):
    print(message)

If the first message comes from channel2 and the second message from channel1, the first message will be received immediately, and the second message will be received as soon as it is available.

This can be helpful when you just need to receive messages and don't care about where are they coming from specifically. If you need to know where the message came from, you can use select() instead.

Stopping¤

A merge receiver will be stopped automatically when all the receivers that it merges are stopped. When using the async iterator interface, this means that the iterator will stop as soon as all the receivers are stopped. When using receive(), this means that the method will raise a ReceiverStoppedError exception as soon as all the receivers are stopped.

If you want to stop a merge receiver manually, you can use the stop() method.

When using receive(), you should make sure to either stop all the receivers that you are merging, or to stop the merge receiver manually. This is to make sure that all the tasks created by the merge receiver are cleaned up properly.