From b1f46cef9e8e1fb6520b7d498901099248bcd018 Mon Sep 17 00:00:00 2001 From: Claudio-Daniel Freire Date: Thu, 18 Aug 2011 17:15:29 +0200 Subject: [PATCH] Fix threadcache to reset itself upon fork. Cached threads are no longer alive after a fork --- src/nepi/util/parallel.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/nepi/util/parallel.py b/src/nepi/util/parallel.py index 015c5704..bd0616ca 100644 --- a/src/nepi/util/parallel.py +++ b/src/nepi/util/parallel.py @@ -5,10 +5,12 @@ import threading import Queue import traceback import sys +import os N_PROCS = None THREADCACHE = [] +THREADCACHEPID = None class WorkerThread(threading.Thread): class QUIT: @@ -79,6 +81,8 @@ class WorkerThread(threading.Thread): class ParallelMap(object): def __init__(self, maxthreads = None, maxqueue = None, results = True): global N_PROCS + global THREADCACHE + global THREADCACHEPID if maxthreads is None: if N_PROCS is None: @@ -103,6 +107,11 @@ class ParallelMap(object): self.rvqueue = Queue.Queue() else: self.rvqueue = None + + # Check threadcache + if THREADCACHEPID is None or THREADCACHEPID != os.getpid(): + del THREADCACHE[:] + THREADCACHEPID = os.getpid() self.workers = [] for x in xrange(maxthreads): @@ -124,6 +133,13 @@ class ParallelMap(object): self.destroy() def destroy(self): + # Check threadcache + global THREADCACHE + global THREADCACHEPID + if THREADCACHEPID is None or THREADCACHEPID != os.getpid(): + del THREADCACHE[:] + THREADCACHEPID = os.getpid() + for worker in self.workers: worker.waitdone() for worker in self.workers: -- 2.43.0