本文整理汇总了Python中twisted.python.threadpool.ThreadPool类的典型用法代码示例。如果您正苦于以下问题:Python ThreadPool类的具体用法?Python ThreadPool怎么用?Python ThreadPool使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ThreadPool类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: __init__
def __init__(self):
from twisted.internet import reactor # Imported here.inside
self.reactor = reactor
engine = get_engine()
# create_schema(engine)
self.read_pool = ThreadPool(
minthreads=1, maxthreads=16, name="ReadPool")
self.write_pool = ThreadPool(
minthreads=1, maxthreads=1, name="WritePool")
self.read_pool.start()
self.write_pool.start()
self.signals = SignalManager(dispatcher.Any).connect(
self.stop_threadpools, spider_closed)
self.counters = defaultdict(lambda: Counter())
self.cache = defaultdict(
lambda: dict())
self.write_queue = Queue()
self.writelock = False # Write queue mutex
开发者ID:codervince,项目名称:racedaylive,代码行数:27,代码来源:pipeline2.py
示例2: makeService
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
开发者ID:UfSoft,项目名称:SSHg,代码行数:33,代码来源:service.py
示例3: ClockWithThreads
class ClockWithThreads(Clock):
"""
A testing reactor that supplies L{IReactorTime} and L{IReactorThreads}.
"""
def __init__(self):
super(ClockWithThreads, self).__init__()
self._pool = ThreadPool()
def getThreadPool(self):
"""
Get the threadpool.
"""
return self._pool
def suggestThreadPoolSize(self, size):
"""
Approximate the behavior of a 'real' reactor.
"""
self._pool.adjustPoolsize(maxthreads=size)
def callInThread(self, thunk, *a, **kw):
"""
No implementation.
"""
def callFromThread(self, thunk, *a, **kw):
"""
开发者ID:anemitz,项目名称:calendarserver,代码行数:32,代码来源:fixtures.py
示例4: __init__
def __init__(self):
minthreads = config["jobtype_logging_threadpool"]["min_threads"]
maxthreads = config["jobtype_logging_threadpool"]["max_threads"]
self.max_queued_lines = \
config["jobtype_logging_threadpool"]["max_queue_size"]
self.flush_lines = \
config["jobtype_logging_threadpool"]["flush_lines"]
self.stopped = False
if minthreads < 1:
raise ValueError(
"Config value "
"jobtype_logging_threadpool.min_threads must be >= 1")
# Calculate maxthreads if a value was not provided for us
if maxthreads == "auto":
auto_maxthreads = min(int(cpu.total_cpus() * 1.5), 20)
maxthreads = max(auto_maxthreads, minthreads)
if minthreads > maxthreads:
raise ValueError(
"Config value jobtype_logging_threadpool.min_threads cannot "
"be larger than jobtype_logging_threadpool.max_threads")
ThreadPool.__init__(
self,
minthreads=minthreads, maxthreads=maxthreads,
name=self.__class__.__name__)
开发者ID:xlhtc007,项目名称:pyfarm-agent,代码行数:28,代码来源:log.py
示例5: from_config
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"],
)
开发者ID:department-of-veterans-affairs,项目名称:efolder-express,代码行数:31,代码来源:app.py
示例6: FakeReactor
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()
开发者ID:Almad,项目名称:twisted,代码行数:29,代码来源:test_base.py
示例7: __init__
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)
开发者ID:ojii,项目名称:ircbotframework,代码行数:7,代码来源:http.py
示例8: NoGoStatusCodes
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)
开发者ID:ckaye89,项目名称:hendrix,代码行数:60,代码来源:test_crosstown_traffic.py
示例9: test_contemporaneous_requests
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
开发者ID:citruspi,项目名称:hendrix,代码行数:59,代码来源:test_crosstown_traffic.py
示例10: __init__
def __init__(self, engine, maxthreads=10, verbose=False):
if engine.dialect.name == 'sqlite':
ThreadPool.__init__(self, minthreads=1, maxthreads=1)
else:
ThreadPool.__init__(self, maxthreads=maxthreads)
self.engine = engine
reactor.callWhenRunning(self.start)
开发者ID:merveunlu,项目名称:neurons,代码行数:8,代码来源:store.py
示例11: _test_StreamingServer
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()
开发者ID:provegard,项目名称:pyupnp,代码行数:58,代码来源:test_upnp.py
示例12: run
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)
开发者ID:brunosmmm,项目名称:hbussd,代码行数:9,代码来源:web.py
示例13: start
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)
开发者ID:xlhtc007,项目名称:pyfarm-agent,代码行数:9,代码来源:log.py
示例14: __init__
def __init__(self, engine, verbose=False):
if engine.dialect.name == "sqlite":
pool_size = 1
ThreadPool.__init__(self, minthreads=1, maxthreads=1)
else:
ThreadPool.__init__(self)
self.engine = engine
reactor.callWhenRunning(self.start)
开发者ID:restofsun,项目名称:spyne,代码行数:10,代码来源:web.py
示例15: twisted
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()
开发者ID:cccaballero,项目名称:web2py,代码行数:10,代码来源:anyserver.py
示例16: _worker
def _worker(self):
self.workerThread = threading.current_thread()
assert self.session is None
self.session = self.context.db._createSession()
try:
ThreadPool._worker(self)
finally:
assert self.session is not None
self.session.commit()
self.session.close()
开发者ID:opencv-infrastructure,项目名称:common-pullrequest-plugin,代码行数:10,代码来源:database.py
示例17: __init__
def __init__(self):
log.debug("[D] %s %s " % (__file__, __name__), "Starting db_threadpool")
self.db_threadpool = ThreadPool(0, config.advanced.db_thread_pool_size)
self.db_threadpool.start()
log.debug("[D] %s %s " % (__file__, __name__), "Starting scheduler_threadpool")
self.scheduler_threadpool = ThreadPool(0, config.advanced.scheduler_thread_pool_size)
self.scheduler_threadpool.start()
self.transactor = Transactor(self.db_threadpool)
self.transactor.retries = 0
开发者ID:hellais,项目名称:GLBackend,代码行数:11,代码来源:__init__.py
示例18: ThreadPoolService
class ThreadPoolService(Service):
def __init__(self):
self.threadpool = ThreadPool()
def startService(self):
self.threadpool.start()
def stopService(self):
self.threadpool.stop()
开发者ID:ddormer,项目名称:bdm,代码行数:11,代码来源:blood_service.py
示例19: SameOrDifferentThread
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)
开发者ID:hendrix,项目名称:hendrix,代码行数:52,代码来源:test_crosstown_traffic.py
示例20: twisted_adapter
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()
开发者ID:FacundoM,项目名称:itty,代码行数:13,代码来源:itty.py
注:本文中的twisted.python.threadpool.ThreadPool类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论