Skip to content

Commit

Permalink
Merge pull request #34 from jepler/improve-await-never
Browse files Browse the repository at this point in the history
Rework how the never singleton is invoked
  • Loading branch information
dhalbert authored Nov 10, 2022
2 parents 0c36d81 + 378b4fb commit a044da1
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
15 changes: 8 additions & 7 deletions asyncio/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ def sleep(t):

################################################################################
# "Never schedule" object"
# Don't re-schedule the object that awaits the _never singleton.
# Don't re-schedule the object that awaits _never().
# For internal use only. Some constructs, like `await event.wait()`,
# work by NOT re-scheduling the task which calls wait(), but by
# having some other task schedule it later.
class _Never:
class _NeverSingletonGenerator:
def __init__(self):
self.state = None
self.exc = StopIteration()
Expand All @@ -117,7 +117,10 @@ def __next__(self):
self.exc.__traceback__ = None
raise self.exc

_never = _Never()
def _never(sgen=_NeverSingletonGenerator()):
# assert sgen.state is None, "Check for a missing `await` in your code"
sgen.state = False
return sgen


################################################################################
Expand Down Expand Up @@ -150,13 +153,11 @@ def _dequeue(self, s):

async def queue_read(self, s):
self._enqueue(s, 0)
_never.state = False
await _never
await _never()

async def queue_write(self, s):
self._enqueue(s, 1)
_never.state = False
await _never
await _never()

def remove(self, task):
while True:
Expand Down
3 changes: 1 addition & 2 deletions asyncio/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ async def wait(self):
self.waiting.push_head(core.cur_task)
# Set calling task's data to the event's queue so it can be removed if needed
core.cur_task.data = self.waiting
core._never.state = False
await core._never
await core._never()
return True


Expand Down
3 changes: 1 addition & 2 deletions asyncio/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ async def acquire(self):
# Set calling task's data to the lock's queue so it can be removed if needed
core.cur_task.data = self.waiting
try:
core._never.state = False
await core._never
await core._never()
except core.CancelledError as er:
if self.state == core.cur_task:
# Cancelled while pending on resume, schedule next waiting Task
Expand Down

0 comments on commit a044da1

Please sign in to comment.