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

QueueIterator: speedup via __anext_impl w/wo timeout in __init__ #627

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
17 changes: 17 additions & 0 deletions aio_pika/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,11 @@ def __init__(self, queue: Queue, **kwargs: Any):
self._queue = asyncio.Queue()
self._consume_kwargs = kwargs

if kwargs.get("timeout"):
Copy link
Owner

Choose a reason for hiding this comment

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

Hm... that's should be handled by wait_for implementation: https://github.com/python/cpython/blob/main/Lib/asyncio/tasks.py#L494-L507

Copy link
Author

Choose a reason for hiding this comment

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

yep... and asyncio.wait_for calls:
https://github.com/python/cpython/blob/main/Lib/asyncio/timeouts.py#L145-L162 and so on which gives ~5% overhead even if we don't use timeout= argument.
my idea is to eliminate this calls' chain if we don't need it

self.__anext_impl = self.__anext_with_timeout
else:
self.__anext_impl = self.__anext_without_timeout

self._amqp_queue.close_callbacks.add(self.close)

async def on_message(self, message: AbstractIncomingMessage) -> None:
Expand Down Expand Up @@ -511,6 +516,9 @@ async def __aexit__(
await self.close()

async def __anext__(self) -> IncomingMessage:
return await self.__anext_impl()

async def __anext_with_timeout(self) -> IncomingMessage:
if not hasattr(self, "_consumer_tag"):
await self.consume()
try:
Expand All @@ -530,5 +538,14 @@ async def __anext__(self) -> IncomingMessage:
await asyncio.wait_for(self.close(), timeout=timeout)
raise

async def __anext_without_timeout(self) -> IncomingMessage:
if not hasattr(self, "_consumer_tag"):
await self.consume()
try:
return await self._queue.get()
except asyncio.CancelledError:
await self.close()
raise


__all__ = ("Queue", "QueueIterator", "ConsumerTag")
Loading