Skip to content

Commit 5a5e9b9

Browse files
committed
Make thread pools work without multiprocessing semaphores
1 parent 3156e0d commit 5a5e9b9

1 file changed

Lines changed: 26 additions & 1 deletion

File tree

  • graalpython/lib-python/3/multiprocessing

graalpython/lib-python/3/multiprocessing/pool.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,30 @@ def _set(self, i, obj):
919919
#
920920
#
921921

922+
# GraalPy change: ThreadPool would still have used semaphore-based
923+
# multiprocessing lock. Use a threading lock to make thread pools work
924+
# on emulated posix backend
925+
class _ThreadPoolContext:
926+
927+
def __init__(self, ctx):
928+
self._ctx = ctx
929+
930+
def __getattr__(self, name):
931+
return getattr(self._ctx, name)
932+
933+
def get_context(self, method=None):
934+
if method is None:
935+
return self
936+
return self._ctx.get_context(method)
937+
938+
def Lock(self):
939+
return threading.Lock()
940+
941+
def SimpleQueue(self):
942+
from .queues import SimpleQueue
943+
return SimpleQueue(ctx=self)
944+
945+
922946
class ThreadPool(Pool):
923947
_wrap_exception = False
924948

@@ -928,7 +952,8 @@ def Process(ctx, *args, **kwds):
928952
return Process(*args, **kwds)
929953

930954
def __init__(self, processes=None, initializer=None, initargs=()):
931-
Pool.__init__(self, processes, initializer, initargs)
955+
# GraalPy change
956+
Pool.__init__(self, processes, initializer, initargs, context=_ThreadPoolContext(get_context()))
932957

933958
def _setup_queues(self):
934959
self._inqueue = queue.SimpleQueue()

0 commit comments

Comments
 (0)