f | import asyncio | f | import asyncio |
| | | |
| class FilterQueue(asyncio.Queue): | | class FilterQueue(asyncio.Queue): |
| | | |
n | def __init__(self, *a, **b): | n | def __init__(self, *args, **kwargs): |
| super().__init__(*a, **b) | | super().__init__(*args, **kwargs) |
| self.window = None | | self.window = None |
| | | |
n | def _put(self, y): | n | def _put(self, item): |
| self._queue.append(y) | | self._queue.append(item) |
| if self.window is None: | | if self.window is None: |
n | self.window = y | n | self.window = item |
| | | |
n | def __contains__(self, c): | n | def __contains__(self, checker): |
| for d in self._queue: | | for item in self._queue: |
| if c(d): | | if checker(item): |
| return True | | return True |
| return False | | return False |
| | | |
| def _get(self): | | def _get(self): |
n | e = self._queue.popleft() | n | item = self._queue.popleft() |
| if self._queue: | | if self._queue: |
| self.window = self._queue[0] | | self.window = self._queue[0] |
| else: | | else: |
| self.window = None | | self.window = None |
n | return e | n | return item |
| | | |
| def later(self): | | def later(self): |
| if not self._queue: | | if not self._queue: |
| self.window = None | | self.window = None |
| raise asyncio.QueueEmpty | | raise asyncio.QueueEmpty |
n | g = self._get() | n | item = self._get() |
| self._put(g) | | self._put(item) |
| | | |
n | async def get(self, h=None): | n | async def get(self, checker=None): |
| if h is not None and h in self: | | if checker is not None and checker in self: |
| while not h(self.window): | | while not checker(self.window): |
| self.later() | | self.later() |
| while self.empty(): | | while self.empty(): |
n | i = self._get_loop().create_future() | n | future = self._get_loop().create_future() |
| self._getters.append(i) | | self._getters.append(future) |
| try: | | try: |
n | await i | n | await future |
| except: | | except: |
n | i.cancel() | n | future.cancel() |
| try: | | try: |
n | self._getters.remove(i) | n | self._getters.remove(future) |
| except ValueError: | | except ValueError: |
| pass | | pass |
t | if not self.empty() and (not i.cancelled()): | t | if not self.empty() and (not future.cancelled()): |
| self._wakeup_next(self._getters) | | self._wakeup_next(self._getters) |
| raise | | raise |
| return self.get_nowait() | | return self.get_nowait() |