Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why do we need await asyncio.Future() after await queue.consume()? #592

Open
IgelVV opened this issue Oct 19, 2023 · 1 comment
Open

Why do we need await asyncio.Future() after await queue.consume()? #592

IgelVV opened this issue Oct 19, 2023 · 1 comment

Comments

@IgelVV
Copy link

IgelVV commented Oct 19, 2023

async def main() -> None:
    connection = await connect("amqp://guest:guest@localhost/")
    async with connection:
        channel = await connection.channel()

        queue = await channel.declare_queue("hello")

        # Start listening the queue with name 'hello' (What does it exactly do?)
        await queue.consume(on_message, no_ack=True)

        print(" [*] Waiting for messages. To exit press CTRL+C")

        #Why we must keep the loop running? 
        #(for example using  run_forever() in other examples after main())
        await asyncio.Future()


if __name__ == "__main__":
    asyncio.run(main())

What does exactly await queue.consume() do?
Why do we need to keep the loop running, it looks like consume() has to.
I tried to walk through the code and found creating unavailable tasks or smth like that, but I can't.

I will be very glad if you help me figure it out, thank you in advance. You're making a great library.

@IgelVV IgelVV changed the title Why do we need await asyncio.Future() after await queue.consume()? Why do we need await asyncio.Future() after await queue.consume()? Oct 19, 2023
@nidemidovich
Copy link

This is necessary to prevent the completion of the main execution. await asyncio.Future() switches the event loop to perform another task. Since the result is not directly set in the Future instance, the event loop will execute main forever. Of course, untill you press Ctrl+C

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants