Tornado IO loop process pool with message passing
Project description
Looppool is a Python 3 package for running process pool of Tornado IO loops. It’s useful for heavy asynchronous applications which don’t fit into single process due to increasing CPU usage and IO loop blocking (see set_blocking_log_threshold).
Install
pip install Looppool
Use
#!/usr/bin/env python3
import looppool
from tornado import gen, ioloop, httpclient
class FetchWorker(looppool.SafeWorker):
_http_client = None
'''Tornado asynchronous HTTP client'''
def _initialise(self):
self._http_client = httpclient.AsyncHTTPClient()
@gen.coroutine
def _process_message(self, url):
try:
response = yield self._http_client.fetch(url)
self._result_queue.put_nowait((url, response.headers.get('server')))
finally:
self._task_queue.task_done()
@gen.coroutine
def main():
loop = ioloop.IOLoop.instance()
pool = looppool.Pool(loop, pool_size = 4, worker_class = FetchWorker)
pool.process_message = print
pool.start()
urls = [
'https://python.org/',
'http://tornadoweb.org/',
'https://google.com/',
'https://stackoverflow.com/',
]
list(map(pool.put_nowait, urls))
pool.stop()
if __name__ == '__main__':
ioloop.IOLoop.instance().run_sync(main)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Looppool-0.1.0.tar.gz
(5.3 kB
view hashes)