Created
August 6, 2018 15:05
-
-
Save jmfrank63/5fb9909a8e06c91dead9265cab2f33de to your computer and use it in GitHub Desktop.
Async deque in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
class AsyncDeque(deque): | |
def __init__(self, elements): | |
super().__init__(elements) | |
def __aiter__(self): | |
return self | |
async def __anext__(self): | |
if not self: | |
raise StopAsyncIteration | |
element = self.popleft() | |
return element |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very cool! If you're interested in allowing consumers to wait while the queue is empty, and/or if you want context manager support, then try this:
Here's an example of how to use it: