def __init__(self, uri): if isinstance(uri, basestring): uri = URI(uri) elif not isinstance(uri, URI): raise TypeError("expected Pyro URI") self._pyroUri = uri self._pyroConnection = None self._pyroOneway = set() self.__pyroTimeout = Pyro.config.COMMTIMEOUT self.__pyroLock = threadutil.Lock()
class ThreadPool(set): lock = threadutil.Lock() def __init__(self, server, callback): self.__server = server self.__callback = callback self.__working = 0 self.__lastshrink = time.time() def attemptRemove(self, member): with self.lock: if len(self) > Pyro.config.THREADPOOL_MINTHREADS: super(ThreadPool, self).remove(member) return True return False def remove(self, member): with self.lock: try: super(ThreadPool, self).remove(member) except KeyError: pass def attemptSpawn(self): with self.lock: if len(self) < Pyro.config.THREADPOOL_MAXTHREADS: worker = SocketWorker(self.__server, self.__callback) self.add(worker) worker.start() return True return False def poolCritical(self): idle = len(self) - self.__working return idle <= 0 def updateWorking(self, number): self.shrink() with self.lock: self.__working += number def shrink(self): threads = len(self) if threads > Pyro.config.THREADPOOL_MINTHREADS: idle = threads - self.__working if idle > Pyro.config.THREADPOOL_MINTHREADS and ( time.time() - self.__lastshrink) > Pyro.config.THREADPOOL_IDLETIMEOUT: for _ in range(idle - Pyro.config.THREADPOOL_MINTHREADS): self.__server.workqueue.put( (None, None) ) # put a 'stop' sentinel in the worker queue to kill a worker self.__lastshrink = time.time()
def __setstate__(self, state): self._pyroUri, self._pyroOneway, self._pyroSerializer, self.__pyroTimeout = state self._pyroConnection = None self.__pyroLock = threadutil.Lock()