创建用于龙卷风 IOloop 的 ZMQStream

Creating a ZMQStream for using in tornado IOloop

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

*** TypeError: argument must be an int, or have a fileno() method.

在尝试创建 ZMQStream 对象时,我读到在未安装 zmq ioloop 时会发生这种情况,但我在程序的第一行从 zmq 调用 ioloop.install()。调用 zmq.eventloop.ioloop.IOLoop.instance() 和龙卷风 IOLoop.instance() returns 同一个对象。

有趣的代码是:

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)

我尝试过的其他事情:

zmq.ioloop.install()的调用需要更早。在debug模式下创建Application会初始化默认的IOLoop,所以在构建Application.

之前必须安装ZMQ IOLoop