def _pool(self, currentLimit, threadFactory):
        """
        Override testing hook to create a deterministic threadpool.

        @param currentLimit: A 1-argument callable which returns the current
            threadpool size limit.

        @param threadFactory: ignored in this invocation; a 0-argument callable
            that would produce a thread.

        @return: a L{Team} backed by the coordinator and worker passed to
            L{MemoryPool.__init__}.
        """
        def respectLimit():
            # The expression in this method copied and pasted from
            # twisted.threads._pool, which is unfortunately bound up
            # with lots of actual-threading stuff.
            stats = team.statistics()
            if ((stats.busyWorkerCount + stats.idleWorkerCount) >=
                    currentLimit()):
                return None
            return self._newWorker()

        team = Team(coordinator=self._coordinator,
                    createWorker=respectLimit,
                    logException=self._failTest)
        return team
Esempio n. 2
0
def deterministicPool():
    """
    Create a deterministic threadpool.

    @return: 2-tuple of L{ThreadPool}, 0-argument C{work} callable; when
        C{work} is called, do the work.
    """
    worker, doer = createMemoryWorker()
    return (DeterministicThreadPool(
        Team(LockWorker(Lock(), local()), (lambda: worker),
             lambda: None)), doer)