Replies: 1 comment 3 replies
-
The following script works fine import asyncio
from faststream import Context, FastStream
from faststream.rabbit import ExchangeType, RabbitBroker, RabbitExchange
exch = RabbitExchange("my_exchange", auto_delete=False, type=ExchangeType.TOPIC)
broker_object = RabbitBroker()
routing_key = "some.routing.key"
app = FastStream(broker_object)
@broker_object.subscriber("test-queue")
async def process_chunk(data, broker=Context()):
msg_dict = data
await broker.publish(msg_dict, routing_key=routing_key, exchange=exch)
async def main():
loop = asyncio.get_running_loop()
on_con_lost = loop.create_future()
await broker_object.start()
await broker_object.publish("hi", "test-queue")
try:
await on_con_lost
finally:
await broker_object.close()
asyncio.run(main()) But the following one is preffered @broker_object.subscriber("test-queue")
@broker_object.publisher(routing_key=routing_key, exchange=exch)
async def process_chunk(data):
return data |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm trying to use asyncio to listen to a remote server, receive data constantly, and then to do some basic parsing and publishing to rabbitmq.
Here's some dummy code:
I keep seeing that the recommendation is to use Context and decorators to pass the broker object into functions/classes that are outside the scope of the faststream code but I'm having a hard time implementing this. When I run this code (well, the code that actually has all the config and functions) I get:
Am I misunderstanding something from the context documentation? What am I actually doing when I write broker=Context() in the def?
This is my first stab at asynchronous code so I might be missing something basic here...
Beta Was this translation helpful? Give feedback.
All reactions