Skip to content

Commit

Permalink
Merge pull request #179 from pipermerriam/piper/ensure-asyncio-Endpoi…
Browse files Browse the repository at this point in the history
…nt.stream-implementation-yields-to-event-loop

Ensure asyncio Endpoint.stream implementation yields to event loop
  • Loading branch information
pipermerriam committed Sep 4, 2020
2 parents 607262e + e2846ab commit e4f150f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lahja/asyncio/endpoint.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import asyncio
from asyncio import StreamReader, StreamWriter
from asyncio import QueueEmpty, StreamReader, StreamWriter
from collections import defaultdict
import functools
import itertools
Expand Down Expand Up @@ -756,7 +756,15 @@ async def stream(
try:
for _ in iterations:
try:
yield await queue.get()
try:
yield queue.get_nowait()
except QueueEmpty:
yield await queue.get()
else:
# We sleep here to yield to the event loop in the case
# that we were able to get an item from the queue
# without an actual `await`
await asyncio.sleep(0)
except GeneratorExit:
break
finally:
Expand Down
1 change: 1 addition & 0 deletions newsfragments/179.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Ensure that the ``asyncio`` implementation releases control of the event loop on each iteration of the async iterator produced by `EndpointAPI.stream`

0 comments on commit e4f150f

Please sign in to comment.