本文整理汇总了Python中twisted.python.threadpool.ThreadPool.start方法的典型用法代码示例。如果您正苦于以下问题:Python ThreadPool.start方法的具体用法?Python ThreadPool.start怎么用?Python ThreadPool.start使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.python.threadpool.ThreadPool
的用法示例。
在下文中一共展示了ThreadPool.start方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from twisted.python.threadpool import ThreadPool [as 别名]
# 或者: from twisted.python.threadpool.ThreadPool import start [as 别名]
def __init__(self, core):
thread_pool = ThreadPool()
thread_pool.start()
reactor.addSystemEventTrigger("after", "shutdown", thread_pool.stop)
application = get_flask_application(core)
wsgi_resource = WSGIResource(reactor, thread_pool, application)
Site.__init__(self, wsgi_resource)
示例2: makeService
# 需要导入模块: from twisted.python.threadpool import ThreadPool [as 别名]
# 或者: from twisted.python.threadpool.ThreadPool import start [as 别名]
def makeService(self, options):
app = Application("Mercurial SSH Server") #, uid, gid)
services = IServiceCollection(app)
service = options.subOptions.getService()
service.setServiceParent(services)
wsgi_app = WSGIApplication()
threadpool = ThreadPool(config.web.min_threads, config.web.max_threads)
threadpool.start()
reactor.addSystemEventTrigger('after', 'shutdown', threadpool.stop)
root = wsgi.WSGIResource(reactor, threadpool, wsgi_app)
factory = server.Site(root)
if isfile(config.web.certificate):
from OpenSSL import SSL
# Run SSL Server
class SSLContext(object):
def getContext(self):
ctx = SSL.Context(SSL.SSLv23_METHOD)
ctx.use_privatekey_file(config.private_key)
ctx.use_certificate_file(config.web.certificate)
return ctx
config_service = internet.SSLServer(config.web.port, factory,
SSLContext())
else:
config_service = internet.TCPServer(config.web.port, factory)
config_service.setServiceParent(app)
clean_changes_task = task.LoopingCall(self.__clean_old_changes_from_db)
clean_changes_task.start(5*60, now=True) # Every 5 minutes
reactor.addSystemEventTrigger('after', 'shutdown',
clean_changes_task.stop)
return services
示例3: NoGoStatusCodes
# 需要导入模块: from twisted.python.threadpool import ThreadPool [as 别名]
# 或者: from twisted.python.threadpool.ThreadPool import start [as 别名]
class NoGoStatusCodes(TestCase):
def __init__(self, *args, **kwargs):
self.tp = ThreadPool(maxthreads=20)
self.tp.start()
self.resource = HendrixWSGIResource(reactor,
self.tp,
self.wsgi_thing)
self.nameSpace = TestNameSpace()
self.nameSpace.async_thing_complete = Queue()
return super(NoGoStatusCodes, self).__init__(*args, **kwargs)
def setUp(self, *args, **kwargs):
self.addCleanup(self.tp.stop)
super(NoGoStatusCodes, self).setUp(*args, **kwargs)
def wsgi_thing(self, environ, start_response):
start_response('404 NOT FOUND', [('Content-type','text/plain')])
@crosstown_traffic.follow_response(
no_go_status_codes=self.no_go_status_codes,
same_thread=True
)
def long_thing_on_same_thread():
self.nameSpace.async_task_was_run = True
logger.info("No bad status codes; went ahead with async thing.")
return "Nothing."
def test_bad_status_codes_cause_no_go_in_wsgi_response(self):
self.no_go_status_codes = [404, '6xx']
request = DummyRequest('r1')
request.isSecure = lambda: False
request.content = "llamas"
finished = request.notifyFinish()
self.resource.render(request)
# This must wait until the WSGI response is closed.
finished.addCallback(
lambda _: self.assertFalse(
self.nameSpace.async_task_was_run
)
)
def test_bad_status_codes_cause_no_go_flag(self):
through_to_you = crosstown_traffic.follow_response(no_go_status_codes=[418])
through_to_you.status_code = 418
through_to_you.check_status_code_against_no_go_list()
self.assertTrue(through_to_you.no_go)
def test_no_bad_status_codes_are_cool(self):
through_to_you = crosstown_traffic.follow_response(no_go_status_codes=[418])
through_to_you.status_code = 404
through_to_you.check_status_code_against_no_go_list()
self.assertFalse(through_to_you.no_go)
示例4: from_config
# 需要导入模块: from twisted.python.threadpool import ThreadPool [as 别名]
# 或者: from twisted.python.threadpool.ThreadPool import start [as 别名]
def from_config(cls, reactor, logger, queue, config_path):
with open(config_path) as f:
config = yaml.safe_load(f)
# TODO: bump this once alchimia properly handles pinning
thread_pool = ThreadPool(minthreads=1, maxthreads=1)
thread_pool.start()
reactor.addSystemEventTrigger('during', 'shutdown', thread_pool.stop)
return cls(
logger,
DownloadDatabase(reactor, thread_pool, config["db"]["uri"]),
FilePath(config["storage"]["filesystem"]),
fernet.MultiFernet([
fernet.Fernet(key) for key in config["encryption_keys"]
]),
VBMSClient(
reactor,
connect_vbms_path=config["connect_vbms"]["path"],
bundle_path=config["connect_vbms"]["bundle_path"],
endpoint_url=config["vbms"]["endpoint_url"],
keyfile=config["vbms"]["keyfile"],
samlfile=config["vbms"]["samlfile"],
key=config["vbms"].get("key"),
keypass=config["vbms"]["keypass"],
ca_cert=config["vbms"].get("ca_cert"),
client_cert=config["vbms"].get("client_cert"),
),
queue,
config["env"],
)
示例5: AddressResolver
# 需要导入模块: from twisted.python.threadpool import ThreadPool [as 别名]
# 或者: from twisted.python.threadpool.ThreadPool import start [as 别名]
class AddressResolver(object):
pool = None
def __init__(self, minthreads=1, maxthreads=4):
self.pool = ThreadPool(minthreads=minthreads, maxthreads=maxthreads)
# unclosed ThreadPool leads to reactor hangs at shutdown
# this is a problem in many situation, so better enforce pool stop here
reactor.addSystemEventTrigger(
"before", "shutdown", self.pool.stop
)
self.pool.start()
def get_host_by_name(self, address):
d = defer.Deferred()
def func():
try:
reactor.callFromThread(
d.callback, socket.gethostbyname(address)
)
except Exception as e:
reactor.callFromThread(d.errback, e)
self.pool.callInThread(func)
return d
def close(self):
self.pool.stop()
示例6: FakeReactor
# 需要导入模块: from twisted.python.threadpool import ThreadPool [as 别名]
# 或者: from twisted.python.threadpool.ThreadPool import start [as 别名]
class FakeReactor(object):
"""
A fake reactor implementation which just supports enough reactor APIs for
L{ThreadedResolver}.
"""
implements(IReactorTime, IReactorThreads)
def __init__(self):
self._clock = Clock()
self.callLater = self._clock.callLater
self._threadpool = ThreadPool()
self._threadpool.start()
self.getThreadPool = lambda: self._threadpool
self._threadCalls = Queue()
def callFromThread(self, f, *args, **kwargs):
self._threadCalls.put((f, args, kwargs))
def _runThreadCalls(self):
f, args, kwargs = self._threadCalls.get()
f(*args, **kwargs)
def _stop(self):
self._threadpool.stop()
示例7: test_contemporaneous_requests
# 需要导入模块: from twisted.python.threadpool import ThreadPool [as 别名]
# 或者: from twisted.python.threadpool.ThreadPool import start [as 别名]
def test_contemporaneous_requests(self):
'''
We're going to create two request-response cycles here:
Cycle 1 will begin.
Cycle 2 will begin.
Cycle 2 will return.
Cycle 1 will return.
This way, we can prove that the crosstown_traffic created
by cycle 1 is not resolved by the return of cycle 2.
'''
tp = ThreadPool(maxthreads=20)
tp.start()
self.addCleanup(tp.stop)
log.debug("\n\nStarting the two stream stuff.")
request1 = DummyRequest('r1')
request1.isSecure = lambda: False
request1.content = "Nothing really here."
request1.headers['llamas'] = 'dingo'
nameSpace.test_case = self
hr = HendrixWSGIResource(reactor, tp, wsgi_application)
d1 = deferToThreadPool(reactor, tp, hr.render, request1)
request2 = DummyRequest('r2')
request2.isSecure = lambda: False
request2.content = "Nothing really here."
request2.headers['llamas'] = 'dingo'
d2 = deferToThreadPool(reactor, tp, hr.render, request2)
def woah_stop(failure):
nameSpace.async_task_was_done.put_nowait(False)
nameSpace.second_cycle_complete.put_nowait(False)
nameSpace.ready_to_proceed_with_second_cycle.put_nowait(False)
d1.addErrback(woah_stop)
d2.addErrback(woah_stop)
combo_deferred = gatherResults([d1, d2])
def wait_for_queue_resolution():
nameSpace.async_task_was_done.get(True, 3)
combo_deferred.addCallback(
lambda _: deferToThreadPool(reactor, tp, wait_for_queue_resolution)
)
combo_deferred.addCallback(
lambda _: self.assertTrue(nameSpace.async_task_was_run)
)
return combo_deferred
示例8: _test_StreamingServer
# 需要导入模块: from twisted.python.threadpool import ThreadPool [as 别名]
# 或者: from twisted.python.threadpool.ThreadPool import start [as 别名]
def _test_StreamingServer():
expected_id = 'test'
headers = {
#'TimeSeekRange.dlna.org': 'npt=30.000-',
'TimeSeekRange.dlna.org': 'npt=20.000-50.000',
#'Range': 'bytes=5-',
'Connection': 'close',
}
duration = '00:01:00'
content_length = 64 * 1024
interface = '192.168.0.103'
class StreamingServerStub(ByteSeekMixin, TimeSeekMixin, StreamingServer):
def get_content(self, id, environ):
eq_(expected_id, id)
#raise ValueError(repr(environ))
for k in headers:
eq_(headers[k], environ['HTTP_' + k.upper()])
return ContentStub(content_length,
'video/mpeg',
'DLNA.ORG_PN=MPEG_PS_NTSC;DLNA.ORG_OP=11')
from twisted.web import client
class HTTPPageGetter(client.HTTPPageGetter):
handleStatus_206 = lambda self: self.handleStatus_200()
class HTTPClientFactory(client.HTTPClientFactory):
protocol = HTTPPageGetter
def getPageFactory(url, *args, **kwargs):
scheme, host, port, path = client._parse(url)
factory = HTTPClientFactory(url, *args, **kwargs)
reactor.connectTCP(host, port, factory)
return factory
app = StreamingServerStub('test')
mapper = Mapper()
mapper.connect(':id', controller='mt', action='get')
app = RoutesMiddleware(app, mapper)
tpool = ThreadPool()
tpool.start()
resource = upnp.WSGIResource(reactor, tpool, app)
port = reactor.listenTCP(0, server.Site(resource), interface=interface)
port_num = port.socket.getsockname()[1]
url = 'http://%s:%i/%s?duration=%s' % (interface, port_num, expected_id, duration)
factory = getPageFactory(url, headers=headers)
def check_result(contents):
raise ValueError('expected: %d, actual: %d' % (content_length, len(contents)))
#raise ValueError(repr(factory.response_headers))
eq_(content_length, len(contents))
factory.deferred.addCallback(check_result)
reactor.callLater(5, reactor.stop)
reactor.run()
示例9: start
# 需要导入模块: from twisted.python.threadpool import ThreadPool [as 别名]
# 或者: from twisted.python.threadpool.ThreadPool import start [as 别名]
def start(self):
"""
Starts the logger pool and sets up the required shutdown event
trigger which will call :meth:`stop` on exit.
"""
reactor.addSystemEventTrigger("before", "shutdown", self.stop)
ThreadPool.start(self)
logger.debug(
"Started logger thread pool (min=%s, max=%s)", self.min, self.max)
示例10: run
# 需要导入模块: from twisted.python.threadpool import ThreadPool [as 别名]
# 或者: from twisted.python.threadpool.ThreadPool import start [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)
示例11: twisted
# 需要导入模块: from twisted.python.threadpool import ThreadPool [as 别名]
# 或者: from twisted.python.threadpool.ThreadPool import start [as 别名]
def twisted(app, address, **options):
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, app))
reactor.listenTCP(address[1], factory, interface=address[0])
reactor.run()
示例12: ThreadPoolService
# 需要导入模块: from twisted.python.threadpool import ThreadPool [as 别名]
# 或者: from twisted.python.threadpool.ThreadPool import start [as 别名]
class ThreadPoolService(Service):
def __init__(self):
self.threadpool = ThreadPool()
def startService(self):
self.threadpool.start()
def stopService(self):
self.threadpool.stop()
示例13: SameOrDifferentThread
# 需要导入模块: from twisted.python.threadpool import ThreadPool [as 别名]
# 或者: from twisted.python.threadpool.ThreadPool import start [as 别名]
class SameOrDifferentThread(TestCase):
def setUp(self, *args, **kwargs):
self.tp = ThreadPool()
self.tp.start()
self.addCleanup(self.tp.stop)
super(SameOrDifferentThread, self).setUp(*args, **kwargs)
def wsgi_thing(self, environ, start_response):
start_response('200 OK', [('Content-type', 'text/plain')])
nameSpace.this_thread = threading.current_thread()
@crosstown_traffic(
same_thread=self.use_same_thread
)
def long_thing_on_same_thread():
nameSpace.thread_that_is_supposed_to_be_the_same = threading.current_thread()
log.debug("Finished async thing on same thread.")
return [b"Nothing."]
def assert_that_threads_are_the_same(self):
self.assertEqual(
nameSpace.this_thread,
nameSpace.thread_that_is_supposed_to_be_the_same
)
def assert_that_threads_are_different(self):
self.assertNotEqual(nameSpace.this_thread,
nameSpace.thread_that_is_supposed_to_be_different)
def request_same_or_different_thread_thread(self):
hr = HendrixWSGIResource(reactor, self.tp, self.wsgi_thing)
request1 = DummyRequest([b'r1'])
request1.isSecure = lambda: False
request1.content = b"llamas"
request1.client = IPv4Address("TCP", b"50.0.50.0", 5000)
d = deferToThreadPool(reactor, self.tp, hr.render, request1)
d.addCallback(lambda _: request1.notifyFinish())
return d
def test_that_threads_are_the_same(self):
self.use_same_thread = True
d = self.request_same_or_different_thread_thread()
d.addCallback(lambda _: self.assert_that_threads_are_the_same)
return pytest_twisted.blockon(d)
def test_that_threads_are_different(self):
self.use_same_thread = False
d = self.request_same_or_different_thread_thread()
d.addCallback(lambda _: self.assert_that_threads_are_different)
return pytest_twisted.blockon(d)
示例14: twisted_adapter
# 需要导入模块: from twisted.python.threadpool import ThreadPool [as 别名]
# 或者: from twisted.python.threadpool.ThreadPool import start [as 别名]
def twisted_adapter(host, port):
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)
ittyResource = wsgi.WSGIResource(reactor, thread_pool, handle_request)
site = server.Site(ittyResource)
reactor.listenTCP(port, site)
reactor.run()
示例15: run
# 需要导入模块: from twisted.python.threadpool import ThreadPool [as 别名]
# 或者: from twisted.python.threadpool.ThreadPool import start [as 别名]
def run(port):
# Create and start a thread pool,
wsgiThreadPool = ThreadPool()
wsgiThreadPool.start()
service_list = []
b = WebRoot()
global_cfg = getGlobalCFG()
global_cfg["local_web_port"] = port
def loadmodule(ob, packagename):
'''
载入扩展模块
'''
if hasattr(ob, "get_module_config"):
config_dict = ob.get_module_config()
web_define = config_dict.get("webmgt")
if web_define != None:
for (url_str,web_cls) in web_define.items():
print("loaded web: {0} ".format(url_str))
web_obj = web_cls()
web_obj.global_cfg = global_cfg
b.mount(url_str, web_obj)
service_define = config_dict.get("service")
if service_define != None:
for (key, service_cls) in service_define.items():
print("loaded service: {0} ".format(str(service_cls)))
service_obj = service_cls()
service_obj.global_cfg = global_cfg
service_list.append(service_obj)
''' 扫描模块存放目录, 加载扩展模块 '''
dir_list = os.listdir(MODULES_PATH)
from dlutil import util_classutil as clsutil
for directory in dir_list:
path_tmp = "{0}/{1}".format(MODULES_PATH, directory)
module_pack = MODULES_PATH.split('/')[-1] + "." + directory
if os.path.isdir(path_tmp) and path_tmp[0:1] != ".":
pack_name = clsutil.find_class_from_path(path_tmp,
loadmodule,
module_pack)
wsgiAppAsResource = WSGIResource(reactor, wsgiThreadPool, b)
site = server.Site(wsgiAppAsResource)
reactor.listenTCP(port, site)
reactor.run()