创建一个ZMQStream以在tornado IOloop中使用

时间:2015-08-03 15:36:36

标签: python tornado pyzmq

我正在尝试在ZMQ消息通过时向我的龙卷风应用程序添加回调。我得到了:

  

*** TypeError:参数必须是int,或者有fileno()方法。

当尝试创建ZMQStream对象时,我已经读过这种情况,当没有安装zmq ioloop时,我在我的程序的第一行从zmq调用ioloop.install()。调用 zmq.eventloop.ioloop.IOLoop.instance()和tornado IOLoop.instance()返回相同的对象。

有趣的代码是:

launch.py​​

import logging
import os
import tornado
import zmq
from zmq.eventloop import ioloop
from tornado.httpserver import HTTPServer
from web.apiserver import Userform, UploadHandler, UserHandler, LoginHandler

application = tornado.web.Application([
    (r"/upload/(?P<token>\w+)", UploadHandler),
    # .....
], debug=True)

if __name__ == "__main__":
if os.geteuid() != 0:
    exit('Por favor, ejecute como root')
ioloop.install()
logging.basicConfig(filename=r'/tmp/tornado.log', level=logging.INFO)
httpsserver = HTTPServer(application, ssl_options={
    "certfile": "web/server.crt",
    "keyfile": "web/server.key",
})
httpsserver.listen(8888)
zmq.eventloop.IOLoop.instance().start()

server.py

thread_pool = ThreadPoolExecutor(max_workers=4)
context = zmq.Context()
class UploadHandler(RequestHandler):
    # .......
    # after some callback magic we end up here:
    def watch_job(self, ip, port):
        subscriber_socket = context.socket(zmq.SUB)
        subscriber_socket.connect("tcp://%s:%s" % (ip, port))
        subscriber_socket.setsockopt(zmq.SUBSCRIBE, 'client')
        import ipdb; ipdb.set_trace()
        stream_sub = ZMQStream(subscriber_socket, IOLoop.instance()) #<--- Here TypeError
        stream_sub.on_recv(self.check_job_progress)

我尝试过的其他事情:

  • 龙卷风ioloop启动后调用ioloop.install()
  • 在龙卷风ioloop开始之前创建套接字
  • 祈祷

1 个答案:

答案 0 :(得分:2)

zmq.ioloop.install()的调用需要更早。在调试模式下创建Application将初始化默认的IOLoop,因此您必须在构造Application之前安装ZMQ IOLoop。