-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix max_in_flight functionality. (#324)
Currently, it's effectively always 1.
- Loading branch information
Gal Topper
authored
Dec 9, 2021
1 parent
06855c3
commit 6272997
Showing
3 changed files
with
64 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import asyncio | ||
|
||
|
||
class AsyncQueue(asyncio.Queue): | ||
""" | ||
asyncio.Queue with a peek method added. | ||
""" | ||
|
||
async def peek(self): | ||
while self.empty(): | ||
getter = self._loop.create_future() | ||
self._getters.append(getter) | ||
try: | ||
await getter | ||
except: # noqa: E722 | ||
getter.cancel() # Just in case getter is not done yet. | ||
try: | ||
# Clean self._getters from canceled getters. | ||
self._getters.remove(getter) | ||
except ValueError: | ||
# The getter could be removed from self._getters by a | ||
# previous put_nowait call. | ||
pass | ||
if not self.empty() and not getter.cancelled(): | ||
# We were woken up by put_nowait(), but can't take | ||
# the call. Wake up the next in line. | ||
self._wakeup_next(self._getters) | ||
raise | ||
return self.peek_nowait() | ||
|
||
def peek_nowait(self): | ||
if self.empty(): | ||
raise asyncio.QueueEmpty | ||
item = self._peek() | ||
self._wakeup_next(self._putters) | ||
return item | ||
|
||
def _peek(self): | ||
return self._queue[0] |
This file contains 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