Skip to content

Commit ab74504

Browse files
authored
bpo-32576: use queue.SimpleQueue in critical places (#5216)
Where a queue may be invoked from a weakref callback, we need to use the reentrant SimpleQueue.
1 parent 6027802 commit ab74504

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

Lib/concurrent/futures/thread.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def __init__(self, max_workers=None, thread_name_prefix='',
128128
raise TypeError("initializer must be a callable")
129129

130130
self._max_workers = max_workers
131-
self._work_queue = queue.Queue()
131+
self._work_queue = queue.SimpleQueue()
132132
self._threads = set()
133133
self._broken = False
134134
self._shutdown = False

Lib/multiprocessing/pool.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def __init__(self, processes=None, initializer=None, initargs=(),
156156
maxtasksperchild=None, context=None):
157157
self._ctx = context or get_context()
158158
self._setup_queues()
159-
self._taskqueue = queue.Queue()
159+
self._taskqueue = queue.SimpleQueue()
160160
self._cache = {}
161161
self._state = RUN
162162
self._maxtasksperchild = maxtasksperchild
@@ -802,15 +802,18 @@ def __init__(self, processes=None, initializer=None, initargs=()):
802802
Pool.__init__(self, processes, initializer, initargs)
803803

804804
def _setup_queues(self):
805-
self._inqueue = queue.Queue()
806-
self._outqueue = queue.Queue()
805+
self._inqueue = queue.SimpleQueue()
806+
self._outqueue = queue.SimpleQueue()
807807
self._quick_put = self._inqueue.put
808808
self._quick_get = self._outqueue.get
809809

810810
@staticmethod
811811
def _help_stuff_finish(inqueue, task_handler, size):
812-
# put sentinels at head of inqueue to make workers finish
813-
with inqueue.not_empty:
814-
inqueue.queue.clear()
815-
inqueue.queue.extend([None] * size)
816-
inqueue.not_empty.notify_all()
812+
# drain inqueue, and put sentinels at its head to make workers finish
813+
try:
814+
while True:
815+
inqueue.get(block=False)
816+
except queue.Empty:
817+
pass
818+
for i in range(size):
819+
inqueue.put(None)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Use queue.SimpleQueue() in places where it can be invoked from a weakref
2+
callback.

0 commit comments

Comments
 (0)