Skip to content

Commit

Permalink
Explicit method
Browse files Browse the repository at this point in the history
  • Loading branch information
nineteendo committed Jun 23, 2024
1 parent 3bb4e65 commit ba8b134
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 20 deletions.
24 changes: 13 additions & 11 deletions Doc/library/asyncio-queue.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,6 @@ Queue

Number of items allowed in the queue.

.. method:: __aiter__()

Return an :term:`asynchronous iterator` which iterates over
the queue of items until :meth:`shutdown` is called and
continues iteration until the queue is empty.

``shutdown(immediate=True)`` stops iteration immediately.

.. versionadded:: 3.14

.. method:: empty()

Return ``True`` if the queue is empty, ``False`` otherwise.
Expand All @@ -80,6 +70,18 @@ Queue
Return an item if one is immediately available, else raise
:exc:`QueueEmpty`.

.. method:: iter()

.. TODO(Nice Zombies)
.. versionadded:: 3.14

.. method:: iter_nowait()

.. TODO(Nice Zombies)
.. versionadded:: 3.14

.. coroutinemethod:: join()

Block until all items in the queue have been received and processed.
Expand Down Expand Up @@ -202,7 +204,7 @@ concurrent tasks::

async def worker(name, queue):
# Get a "work item" out of the queue.
async for sleep_for in queue:
async for sleep_for in queue.iter():
# Sleep for the "sleep_for" seconds.
await asyncio.sleep(sleep_for)

Expand Down
19 changes: 17 additions & 2 deletions Lib/asyncio/queues.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,6 @@ def _wakeup_next(self, waiters):
waiter.set_result(None)
break

def __aiter__(self):
return _AsyncQueueIterator(self)

def __repr__(self):
return f'<{type(self).__name__} at {id(self):#x} {self._format()}>'
Expand Down Expand Up @@ -233,6 +231,23 @@ def get_nowait(self):
self._wakeup_next(self._putters)
return item

def iter(self):
# TODO(Nice Zombies)
return _AsyncQueueIterator(self)

def iter_nowait(self):
# TODO(Nice Zombies)
try:
yield self.get_nowait()
except QueueShutDown:
return

try:
while True:
yield self.get_nowait()
except (QueueEmpty, QueueShutDown):
return

def task_done(self):
"""Indicate that a formerly enqueued task is complete.
Expand Down
31 changes: 24 additions & 7 deletions Lib/test/test_asyncio/test_queues.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,23 +116,40 @@ async def putter():
self.assertTrue(t.done())
self.assertTrue(t.result())

async def test_aiter(self):
async def test_iter(self):
q = asyncio.Queue()
for i in range(100):
q.put_nowait(i)
accumulator = 0

# All tasks have been queued
q.shutdown()
async def worker():
nonlocal accumulator

async for item in q.iter():
accumulator += item

async with asyncio.TaskGroup() as tg:
tg.create_task(worker())
tg.create_task(worker())
for i in range(100):
q.put_nowait(i)

q.shutdown()

self.assertEqual(sum(range(100)), accumulator)

async def test_iter_nowait(self):
q = asyncio.Queue()
accumulator = 0

async def worker():
nonlocal accumulator

async for item in q:
for item in q.iter_nowait():
accumulator += item

async with asyncio.TaskGroup() as tg:
for i in range(100):
q.put_nowait(i)

tg.create_task(worker())
tg.create_task(worker())

Expand Down Expand Up @@ -497,7 +514,7 @@ async def test_task_done(self):
async def worker():
nonlocal accumulator

async for item in q:
async for item in q.iter():
accumulator += item
q.task_done()

Expand Down

0 comments on commit ba8b134

Please sign in to comment.