Skip to content

Parallel RPC with asyncio.gather #1173

Answered by Lancetnik
PhoenixNazarov asked this question in Q&A
Discussion options

You must be logged in to vote

@PhoenixNazarov please, do not try to connect broker concurantly: it can leads to race condition.

The following code should works fine

import asyncio
from faststream.rabbit import RabbitBroker, RabbitExchange, RabbitQueue

exchange = RabbitExchange('ai')
queue = RabbitQueue('test')

async def t():
    async with RabbitBroker() as broker:
        result = await asyncio.gather(
            send(1, broker),
            send(2, broker),
        )


async def send(num, broker):
    out = await broker.publish(num, queue=queue, exchange=exchange, rpc=True)
    print(out)

asyncio.run(t())

Replies: 1 comment 7 replies

Comment options

You must be logged in to vote
7 replies
@PhoenixNazarov
Comment options

@Lancetnik
Comment options

Answer selected by PhoenixNazarov
@PhoenixNazarov
Comment options

@Lancetnik
Comment options

@PhoenixNazarov
Comment options

@Lancetnik
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants