python tornado 통합 redis 메시지 구독 비동기 작업 후 tornado 메 인 프로그램 을 시작 할 수 없습니다.
CacheQueue1
import redis
import logging
class CacheQueue(object):
def __init__(self, host, port, cache_update_path):
self._pool_cache = redis.ConnectionPool(host=host, port=port, db=0)
@gen.coroutine
def listen():
r = redis.StrictRedis(connection_pool=self._pool_cache)
p = r.pubsub()
p.subscribe("cache_update_path")
for item in p.listen():
if item['type'] == 'message':
logging.info(item['data'])
CacheQueue2
import redis
import logging
from threading import Thread
class CacheQueue(object):
def __init__(self, host, port, cache_update_path):
self._pool_cache = redis.ConnectionPool(host=host, port=port, db=0)
def async_listen():
thead_sub = Thread(target=self.listen, args=(byte,))
thead_sub.start()
def listen():
r = redis.StrictRedis(connection_pool=self._pool_cache)
p = r.pubsub()
p.subscribe("cache_update_path")
for item in p.listen():
if item['type'] == 'message':
logging.info(item['data'])
CacheQueue3
import redis
import logging
from concurrent import futures
executor = futures.ThreadPoolExecutor(max_workers=1)
class CacheQueue(object):
def __init__(self, host, port, cache_update_path):
self._pool_cache = redis.ConnectionPool(host=host, port=port, db=10)
self.cache_update_path =cache_update_path
def _listen(self,pool_cache):
logging.info('async ==> redis_cache')
r = redis.StrictRedis(connection_pool=pool_cache)
p = r.pubsub()
p.subscribe("cache_update_path")
for item in p.listen():
logging.info('get publish content')
print item
if item['type'] == 'message':
logging.info(item['data'])
self.cache_update_path.add(item['data'])
def async_listen(self):
executor.submit(self._listen, self._pool_cache)
호출 코드 세 션
class Application(tornado.web.Application):
def __init__(self, settings=None, script=None):
self.cache_queue = CacheQueue(host=self.settings.get('redis_host', 'localhost'),
port=self.settings.get('redis_port', 6379),
cache_update_path=self.cache_update_path)
tornado.web.Application.__init__(self, handlers, **self.settings)
# : redis ,tornado
#self.cache_queue.listen() # CacheQueue 1 :
# CacheQueue 2 :
# CacheQueue 3 :
self.cache_queue.async_listen()
def main():
...
app = Application(settings=settings, script=script)
app.port = port
http_server = tornado.httpserver.HTTPServer(app, xheaders=True)
http_server.listen(options.port)
....
tornado.ioloop.IOLoop.instance().start()
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
집 서버 설계 (하드웨어 편)자신의 Redmine이나 ownCloud를 운용하기 위해 사쿠라 VPS, DigitalOcean, OpenShift 등을 놀랐습니다만, 침착 해 왔으므로 현상을 정리하고 싶습니다. 먼저 하드웨어 구성을 정리합니다. ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.