Skip to content

Rework how the never singleton is invoked #34

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

Merged
merged 1 commit into from
Nov 10, 2022
Merged
Show file tree
Hide file tree
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
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