Skip to content

Commit

Permalink
Merge threadpool-py3-5975-2.
Browse files Browse the repository at this point in the history
Author: itamar
Review: exarkun
Fixes: twisted#5975

Port twisted.python.threadpool to Python 3.


git-svn-id: svn://svn.twistedmatrix.com/svn/Twisted/trunk@35657 bbbe8e31-12d6-0310-92fd-ac37d47ddeeb
  • Loading branch information
itamarst committed Sep 14, 2012
1 parent e34f936 commit 0263fc1
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 28 deletions.
2 changes: 2 additions & 0 deletions admin/_twistedpython3.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"twisted.python.runtime",
"twisted.python.test",
"twisted.python.threadable",
"twisted.python.threadpool",
"twisted.python._utilpy3",
"twisted.python.versions",
"twisted.test",
Expand Down Expand Up @@ -72,6 +73,7 @@
"twisted.test.test_paths",
"twisted.test.test_threadable",
"twisted.test.test_twisted",
"twisted.test.test_threadpool",
"twisted.trial.test.test_assertions",
"twisted.trial.test.test_suppression",
"twisted.trial.test.test_utilpy3",
Expand Down
9 changes: 7 additions & 2 deletions twisted/python/threadpool.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
instead of creating a thread pool directly.
"""

import Queue
from __future__ import division, absolute_import

try:
from Queue import Queue
except ImportError:
from queue import Queue
import threading
import copy

Expand Down Expand Up @@ -47,7 +52,7 @@ def __init__(self, minthreads=5, maxthreads=20, name=None):
"""
assert minthreads >= 0, 'minimum is negative'
assert minthreads <= maxthreads, 'minimum is greater than maximum'
self.q = Queue.Queue(0)
self.q = Queue(0)
self.min = minthreads
self.max = maxthreads
self.name = name
Expand Down
68 changes: 42 additions & 26 deletions twisted/test/test_threadpool.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
Tests for L{twisted.python.threadpool}
"""

from __future__ import division, absolute_import

import pickle, time, weakref, gc, threading

from twisted.python.compat import _PY3
from twisted.trial import unittest
from twisted.python import threadpool, threadable, failure, context
from twisted.internet import reactor
from twisted.internet.defer import Deferred

#
Expand Down Expand Up @@ -55,12 +57,26 @@ def run(self):



class ThreadPoolTestCase(unittest.TestCase):
class ThreadPoolTestCase(unittest.SynchronousTestCase):
"""
Test threadpools.
"""

def getTimeout(self):
"""
Return number of seconds to wait before giving up.
"""
return 5 # Really should be order of magnitude less


def _waitForLock(self, lock):
for i in xrange(1000000):
# We could just use range(), but then we use an extra 30MB of memory
# on Python 2:
if _PY3:
items = range(1000000)
else:
items = xrange(1000000)
for i in items:
if lock.acquire(False):
break
time.sleep(1e-5)
Expand Down Expand Up @@ -240,7 +256,7 @@ def _threadpoolTest(self, method):
waiting.acquire()
actor = Synchronization(N, waiting)

for i in xrange(N):
for i in range(N):
method(tp, actor)

self._waitForLock(waiting)
Expand Down Expand Up @@ -384,16 +400,14 @@ def test_callbackThread(self):
"""
threadIds = []

import thread

event = threading.Event()

def onResult(success, result):
threadIds.append(thread.get_ident())
threadIds.append(threading.currentThread().ident)
event.set()

def func():
threadIds.append(thread.get_ident())
threadIds.append(threading.currentThread().ident)

tp = threadpool.ThreadPool(0, 1)
tp.callInThreadWithCallback(onResult, func)
Expand Down Expand Up @@ -459,7 +473,15 @@ def test_existingWork(self):



class RaceConditionTestCase(unittest.TestCase):
class RaceConditionTestCase(unittest.SynchronousTestCase):

def getTimeout(self):
"""
Return number of seconds to wait before giving up.
"""
return 5 # Really should be order of magnitude less


def setUp(self):
self.event = threading.Event()
self.threadpool = threadpool.ThreadPool(0, 10)
Expand Down Expand Up @@ -505,22 +527,16 @@ def test_singleThread(self):
# Ensure no threads running
self.assertEqual(self.threadpool.workers, 0)

loopDeferred = Deferred()
event = threading.Event()
event.clear()

def onResult(success, counter):
reactor.callFromThread(submit, counter)

def submit(counter):
if counter:
self.threadpool.callInThreadWithCallback(
onResult, lambda: counter - 1)
else:
loopDeferred.callback(None)

def cbLoop(ignored):
# Ensure there is only one thread running.
self.assertEqual(self.threadpool.workers, 1)

loopDeferred.addCallback(cbLoop)
submit(10)
return loopDeferred
event.set()

for i in range(10):
self.threadpool.callInThreadWithCallback(
onResult, lambda: None)
event.wait()
event.clear()

self.assertEqual(self.threadpool.workers, 1)
Empty file added twisted/topfiles/5975.misc
Empty file.

0 comments on commit 0263fc1

Please sign in to comment.