Skip to content

Instantly share code, notes, and snippets.

@jepler
Created August 21, 2023 15:00
Show Gist options
  • Save jepler/71f02ca767d75d5fa4dd40abf70655f9 to your computer and use it in GitHub Desktop.
Save jepler/71f02ca767d75d5fa4dd40abf70655f9 to your computer and use it in GitHub Desktop.
From 511292567ef15b9ee9a4c57e028f54ead08f1be4 Mon Sep 17 00:00:00 2001
From: Jeff Epler <jepler@gmail.com>
Date: Mon, 21 Aug 2023 09:59:54 -0500
Subject: [PATCH] CP 9 & 10 compatibility
---
asyncio/core.py | 22 ++++++++++++++--------
asyncio/event.py | 4 ++--
asyncio/funcs.py | 2 +-
asyncio/lock.py | 6 +++---
asyncio/task.py | 4 ++--
5 files changed, 22 insertions(+), 16 deletions(-)
diff --git a/asyncio/core.py b/asyncio/core.py
index 524cc57..e6da116 100644
--- a/asyncio/core.py
+++ b/asyncio/core.py
@@ -24,6 +24,12 @@ try:
except:
from .task import TaskQueue, Task
+if hasattr(TaskQueue, 'push'):
+ _task_queue_push = TaskQueue.push
+ _task_queue_pop = TaskQueue.pop
+else:
+ _task_queue_push = TaskQueue.push_sorted
+ _task_queue_pop = TaskQueue.pop_head
################################################################################
# Exceptions
@@ -62,7 +68,7 @@ class SingletonGenerator:
def __next__(self):
if self.state is not None:
- _task_queue.push(cur_task, self.state)
+ _task_queue_push(_task_queue, cur_task, self.state)
self.state = None
return None
else:
@@ -179,11 +185,11 @@ class IOQueue:
# print('poll', s, sm, ev)
if ev & ~select.POLLOUT and sm[0] is not None:
# POLLIN or error
- _task_queue.push(sm[0])
+ _task_queue_push(_task_queue, sm[0])
sm[0] = None
if ev & ~select.POLLIN and sm[1] is not None:
# POLLOUT or error
- _task_queue.push(sm[1])
+ _task_queue_push(_task_queue, sm[1])
sm[1] = None
if sm[0] is None and sm[1] is None:
self._dequeue(s)
@@ -211,7 +217,7 @@ def create_task(coro):
if not hasattr(coro, "send"):
raise TypeError("coroutine expected")
t = Task(coro, globals())
- _task_queue.push(t)
+ _task_queue_push(_task_queue, t)
return t
@@ -238,7 +244,7 @@ def run_until_complete(main_task=None):
_io_queue.wait_io_event(dt)
# Get next task to run and continue it
- t = _task_queue.pop()
+ t = _task_queue_pop(_task_queue)
cur_task = t
try:
# Continue running the coroutine, it's responsible for rescheduling itself
@@ -274,7 +280,7 @@ def run_until_complete(main_task=None):
else:
# Schedule any other tasks waiting on the completion of this task.
while t.state.peek():
- _task_queue.push(t.state.pop())
+ _task_queue_push(_task_queue, _task_queue_pop(t.state))
waiting = True
# "False" indicates that the task is complete and has been await'ed on.
t.state = False
@@ -282,7 +288,7 @@ def run_until_complete(main_task=None):
# An exception ended this detached task, so queue it for later
# execution to handle the uncaught exception if no other task retrieves
# the exception in the meantime (this is handled by Task.throw).
- _task_queue.push(t)
+ _task_queue_push(_task_queue, t)
# Save return value of coro to pass up to caller.
t.data = er
elif t.state is None:
@@ -344,7 +350,7 @@ class Loop:
global _stop_task
if _stop_task is not None:
- _task_queue.push(_stop_task)
+ _task_queue_push(_task_queue, _stop_task)
# If stop() is called again, do nothing
_stop_task = None
diff --git a/asyncio/event.py b/asyncio/event.py
index 5e1cb24..c8a78cb 100644
--- a/asyncio/event.py
+++ b/asyncio/event.py
@@ -40,7 +40,7 @@ class Event:
# Note: This must not be called from anything except the thread running
# the asyncio loop (i.e. neither hard or soft IRQ, or a different thread).
while self.waiting.peek():
- core._task_queue.push(self.waiting.pop())
+ core._task_queue_push(core._task_queue, core._task_queue_pop(self.waiting))
self.state = True
def clear(self):
@@ -57,7 +57,7 @@ class Event:
if not self.state:
# Event not set, put the calling task on the event's waiting queue
- self.waiting.push(core.cur_task)
+ core._task_queue_push(self.waiting, 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
await core._never()
diff --git a/asyncio/funcs.py b/asyncio/funcs.py
index dc27088..135b97c 100644
--- a/asyncio/funcs.py
+++ b/asyncio/funcs.py
@@ -116,7 +116,7 @@ async def gather(*aws, return_exceptions=False):
# Still some sub-tasks running.
return
# Gather waiting is done, schedule the main gather task.
- core._task_queue.push(gather_task)
+ core._task_queue_push(core._task_queue, gather_task)
ts = [core._promote_to_task(aw) for aw in aws]
for i in range(len(ts)):
diff --git a/asyncio/lock.py b/asyncio/lock.py
index c9feb35..6df9dba 100644
--- a/asyncio/lock.py
+++ b/asyncio/lock.py
@@ -50,8 +50,8 @@ class Lock:
raise RuntimeError("Lock not acquired")
if self.waiting.peek():
# Task(s) waiting on lock, schedule next Task
- self.state = self.waiting.pop()
- core._task_queue.push(self.state)
+ self.state = core._task_queue_pop(self.waiting)
+ core._task_queue_push(core._task_queue, self.state)
else:
# No Task waiting so unlock
self.state = 0
@@ -65,7 +65,7 @@ class Lock:
if self.state != 0:
# Lock unavailable, put the calling Task on the waiting queue
- self.waiting.push(core.cur_task)
+ core._task_queue_push(self.waiting, core.cur_task)
# Set calling task's data to the lock's queue so it can be removed if needed
core.cur_task.data = self.waiting
try:
diff --git a/asyncio/task.py b/asyncio/task.py
index c1306d0..c6b6070 100644
--- a/asyncio/task.py
+++ b/asyncio/task.py
@@ -202,10 +202,10 @@ class Task:
if hasattr(self.data, "remove"):
# Not on the main running queue, remove the task from the queue it's on.
self.data.remove(self)
- core._task_queue.push(self)
+ core._task_queue_push(self)
elif core.ticks_diff(self.ph_key, core.ticks()) > 0:
# On the main running queue but scheduled in the future, so bring it forward to now.
core._task_queue.remove(self)
- core._task_queue.push(self)
+ core._task_queue_push(self)
self.data = core.CancelledError
return True
--
2.39.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy