本文整理汇总了Python中twisted.python.threadpool.ThreadPool方法的典型用法代码示例。如果您正苦于以下问题:Python threadpool.ThreadPool方法的具体用法?Python threadpool.ThreadPool怎么用?Python threadpool.ThreadPool使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.python.threadpool
的用法示例。
在下文中一共展示了threadpool.ThreadPool方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_wsgi
# 需要导入模块: from twisted.python import threadpool [as 别名]
# 或者: from twisted.python.threadpool import ThreadPool [as 别名]
def test_wsgi(self):
"""
The I{--wsgi} option takes the fully-qualifed Python name of a WSGI
application object and creates a L{WSGIResource} at the root which
serves that application.
"""
options = Options()
options.parseOptions(['--wsgi', __name__ + '.application'])
root = options['root']
self.assertTrue(root, WSGIResource)
self.assertIdentical(root._reactor, reactor)
self.assertTrue(isinstance(root._threadpool, ThreadPool))
self.assertIdentical(root._application, application)
# The threadpool should start and stop with the reactor.
self.assertFalse(root._threadpool.started)
reactor.fireSystemEvent('startup')
self.assertTrue(root._threadpool.started)
self.assertFalse(root._threadpool.joined)
reactor.fireSystemEvent('shutdown')
self.assertTrue(root._threadpool.joined)
示例2: test_callInThreadException
# 需要导入模块: from twisted.python import threadpool [as 别名]
# 或者: from twisted.python.threadpool import ThreadPool [as 别名]
def test_callInThreadException(self):
"""
L{ThreadPool.callInThread} logs exceptions raised by the callable it
is passed.
"""
class NewError(Exception):
pass
def raiseError():
raise NewError()
tp = threadpool.ThreadPool(0, 1)
tp.callInThread(raiseError)
tp.start()
tp.stop()
errors = self.flushLoggedErrors(NewError)
self.assertEqual(len(errors), 1)
示例3: test_existingWork
# 需要导入模块: from twisted.python import threadpool [as 别名]
# 或者: from twisted.python.threadpool import ThreadPool [as 别名]
def test_existingWork(self):
"""
Work added to the threadpool before its start should be executed once
the threadpool is started: this is ensured by trying to release a lock
previously acquired.
"""
waiter = threading.Lock()
waiter.acquire()
tp = threadpool.ThreadPool(0, 1)
tp.callInThread(waiter.release) # before start()
tp.start()
try:
self._waitForLock(waiter)
finally:
tp.stop()
示例4: __init__
# 需要导入模块: from twisted.python import threadpool [as 别名]
# 或者: from twisted.python.threadpool import ThreadPool [as 别名]
def __init__(self, testCase, *args, **kwargs):
"""
Create a L{PoolHelper}.
@param testCase: a test case attached to this helper.
@type args: The arguments passed to a L{threadpool.ThreadPool}.
@type kwargs: The arguments passed to a L{threadpool.ThreadPool}
"""
coordinator, self.performCoordination = createMemoryWorker()
self.workers = []
def newWorker():
self.workers.append(createMemoryWorker())
return self.workers[-1][0]
self.threadpool = MemoryPool(coordinator, testCase.fail, newWorker,
*args, **kwargs)
示例5: test_integration
# 需要导入模块: from twisted.python import threadpool [as 别名]
# 或者: from twisted.python.threadpool import ThreadPool [as 别名]
def test_integration(self, logger):
"""
``auto_threaded`` works with ``twisted.python.threads.ThreadPool``.
"""
from twisted.internet import reactor
threadpool = ThreadPool(minthreads=1, name=self.id())
threadpool.start()
self.addCleanup(threadpool.stop)
spy = Spy()
async_spy = AsyncSpy(
reactor=reactor, threadpool=threadpool, provider=spy
)
a = [object()]
b = [object()]
c = [object()]
with LOG_IN_CALLER():
result = async_spy.method(a, b, c)
result.addCallback(self.assertEqual, spy.method(a, b, c))
return result
示例6: test_existingWork
# 需要导入模块: from twisted.python import threadpool [as 别名]
# 或者: from twisted.python.threadpool import ThreadPool [as 别名]
def test_existingWork(self):
"""
Work added to the threadpool before its start should be executed once
the threadpool is started: this is ensured by trying to release a lock
previously acquired.
"""
waiter = threading.Lock()
waiter.acquire()
tp = threadpool.ThreadPool(0, 1)
tp.callInThread(waiter.release) # Before start()
tp.start()
try:
self._waitForLock(waiter)
finally:
tp.stop()
示例7: __init__
# 需要导入模块: from twisted.python import threadpool [as 别名]
# 或者: from twisted.python.threadpool import ThreadPool [as 别名]
def __init__(self, testCase, *args, **kwargs):
"""
Create a L{PoolHelper}.
@param testCase: a test case attached to this helper.
@type args: The arguments passed to a L{threadpool.ThreadPool}.
@type kwargs: The arguments passed to a L{threadpool.ThreadPool}
"""
coordinator, self.performCoordination = createMemoryWorker()
self.workers = []
def newWorker():
self.workers.append(createMemoryWorker())
return self.workers[-1][0]
self.threadpool = MemoryPool(coordinator, testCase.fail, newWorker,
*args, **kwargs)
示例8: run
# 需要导入模块: from twisted.python import threadpool [as 别名]
# 或者: from twisted.python.threadpool import ThreadPool [as 别名]
def run(self, handler):
from twisted.web import server, wsgi
from twisted.python.threadpool import ThreadPool
from twisted.internet import reactor
thread_pool = ThreadPool()
thread_pool.start()
reactor.addSystemEventTrigger('after', 'shutdown', thread_pool.stop)
factory = server.Site(wsgi.WSGIResource(reactor, thread_pool, handler))
reactor.listenTCP(self.port, factory, interface=self.host)
reactor.run()
示例9: setupService
# 需要导入模块: from twisted.python import threadpool [as 别名]
# 或者: from twisted.python.threadpool import ThreadPool [as 别名]
def setupService(self):
self.log(log.info, u'Bootstrapping')
self.settings = self.parent.settings
# Optionally register subsystem component as child service
for subsystem in self.subsystems:
if hasattr(self, subsystem):
subsystem_service = getattr(self, subsystem)
if isinstance(subsystem_service, Service):
log.info('Registering subsystem component "{subsystem}" as service', subsystem=subsystem)
self.registerService(subsystem_service)
# Configure metrics to be collected each X seconds
metrics_interval = int(self.channel.get('metrics_logger_interval', 60))
self.metrics = Bunch(tx_count=0, starttime=time.time(), interval=metrics_interval)
subscriptions = read_list(self.channel.mqtt_topics)
self.mqtt_service = MqttAdapter(
name = u'mqtt-' + self.channel.realm,
broker_host = self.settings.mqtt.host,
broker_port = int(self.settings.mqtt.port),
broker_username = self.settings.mqtt.username,
broker_password = self.settings.mqtt.password,
callback = self.mqtt_receive,
subscriptions = subscriptions)
self.registerService(self.mqtt_service)
self.influx = InfluxDBAdapter(settings = self.settings.influxdb)
# Perform MQTT message processing using a different thread pool
self.threadpool = ThreadPool()
self.thimble = Thimble(reactor, self.threadpool, self, ["process_message"])
示例10: run
# 需要导入模块: from twisted.python import threadpool [as 别名]
# 或者: from twisted.python.threadpool import ThreadPool [as 别名]
def run(self, handler):
from twisted.web import server, wsgi
from twisted.python.threadpool import ThreadPool
from twisted.internet import reactor
thread_pool = ThreadPool()
thread_pool.start()
reactor.addSystemEventTrigger('after', 'shutdown', thread_pool.stop)
factory = server.Site(wsgi.WSGIResource(reactor, thread_pool, handler))
reactor.listenTCP(self.port, factory, interface=self.host)
if not reactor.running:
reactor.run()
示例11: opt_wsgi
# 需要导入模块: from twisted.python import threadpool [as 别名]
# 或者: from twisted.python.threadpool import ThreadPool [as 别名]
def opt_wsgi(self, name):
"""
The FQPN of a WSGI application object to serve as the root resource of
the webserver.
"""
try:
application = reflect.namedAny(name)
except (AttributeError, ValueError):
raise usage.UsageError("No such WSGI application: %r" % (name,))
pool = threadpool.ThreadPool()
reactor.callWhenRunning(pool.start)
reactor.addSystemEventTrigger('after', 'shutdown', pool.stop)
self['root'] = wsgi.WSGIResource(reactor, pool, application)
示例12: enableThreads
# 需要导入模块: from twisted.python import threadpool [as 别名]
# 或者: from twisted.python.threadpool import ThreadPool [as 别名]
def enableThreads(self):
self.reactor = reactor
self.threadpool = ThreadPool()
self.threadpool.start()
self.addCleanup(self.threadpool.stop)
示例13: _initThreadPool
# 需要导入模块: from twisted.python import threadpool [as 别名]
# 或者: from twisted.python.threadpool import ThreadPool [as 别名]
def _initThreadPool(self):
"""
Create the threadpool accessible with callFromThread.
"""
from twisted.python import threadpool
self.threadpool = threadpool.ThreadPool(
0, 10, 'twisted.internet.reactor')
self._threadpoolStartupID = self.callWhenRunning(
self.threadpool.start)
self.threadpoolShutdownID = self.addSystemEventTrigger(
'during', 'shutdown', self._stopThreadPool)
示例14: deterministicPool
# 需要导入模块: from twisted.python import threadpool [as 别名]
# 或者: from twisted.python.threadpool import ThreadPool [as 别名]
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
)
示例15: test_getThreadPool
# 需要导入模块: from twisted.python import threadpool [as 别名]
# 或者: from twisted.python.threadpool import ThreadPool [as 别名]
def test_getThreadPool(self):
"""
C{reactor.getThreadPool()} returns an instance of L{ThreadPool} which
starts when C{reactor.run()} is called and stops before it returns.
"""
state = []
reactor = self.buildReactor()
pool = reactor.getThreadPool()
self.assertIsInstance(pool, ThreadPool)
self.assertFalse(
pool.started, "Pool should not start before reactor.run")
def f():
# Record the state for later assertions
state.append(pool.started)
state.append(pool.joined)
reactor.stop()
reactor.callWhenRunning(f)
self.runReactor(reactor, 2)
self.assertTrue(
state[0], "Pool should start after reactor.run")
self.assertFalse(
state[1], "Pool should not be joined before reactor.stop")
self.assertTrue(
pool.joined,
"Pool should be stopped after reactor.run returns")