Skip to content
This repository was archived by the owner on May 31, 2021. It is now read-only.

Improve consume/produce examples #10

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions examples/producer_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ async def consume(queue):

loop = asyncio.get_event_loop()
queue = asyncio.Queue(loop=loop)
asyncio.ensure_future(produce(queue, 10), loop=loop)
loop.run_until_complete(consume(queue))
producer_coro = produce(queue, 10)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO it is deliberate to not wait for the completion of the producer.

Copy link
Collaborator

@vxgmichel vxgmichel Oct 17, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the idea here is to run the producer in a background task while waiting for the consumer to complete. However, I'm not opposed to this change if you think it makes the example more understandable. As an alternative, it is possible to make the producer task more explicit:

loop = asyncio.get_event_loop()
queue = asyncio.Queue(loop=loop)
producer_task = loop.create_task(produce(queue, 10))
loop.run_until_complete(consume(queue))
assert producer_task.done()
loop.close()

In any case, it's probably good to discourage the fire and forget approach for the background task.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yesterday, during AsyncIO workshop at PyCON-FR, it was hard for two attendees to understand why we have ensure_future for one coroutine, and not for the other.

I've proposed to change the example as proposed by @sylvainb, it seems more understandable by available beginners.
I've also suggested to propose a pull request for that.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then it's fine with me, I'm merging it ;-)

consumer_coro = consume(queue)
loop.run_until_complete(asyncio.gather(producer_coro, consumer_coro))
loop.close()
4 changes: 4 additions & 0 deletions examples/producer_consumer_join.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@ async def consume(queue):

async def run(n):
queue = asyncio.Queue()
# register the consume coroutine
consumer = asyncio.ensure_future(consume(queue))
# launch the producer and wait for completion
await produce(queue, n)
# ensure the consumer consumes all produced items
await queue.join()
# consumer is always awaiting for a new item, cancel it
consumer.cancel()


Expand Down