【问题标题】:python tornado websocket error: RuntimeError: There is no current event loop in thread 'Thread-1'python tornado websocket错误:RuntimeError:线程'Thread-1'中没有当前事件循环
【发布时间】:2018-10-14 17:31:53
【问题描述】:


我有以下代码,它在 Tornado = 5 运行它就会失败,

import tornado.httpserver
import tornado.ioloop
import threading
import tornado.web,tornado.websocket

class thr(threading.Thread):
    def __init__(self,handler):
        self.handler=handler
        threading.Thread.__init__(self)
    def run(self):
        self.handler.write_message("test")
class ClientWebSocketConnectionHandler(tornado.websocket.WebSocketHandler):
    def open(self):
            print ("opened")
    def on_message(self, message):
            thr(self).start()    

class MainApplication(tornado.web.Application):
    def __init__(self):
        handlers = [(r'/User', ClientWebSocketConnectionHandler),]
        tornado.web.Application.__init__(self, handlers,)

TheShieldsWebSocket = MainApplication()
server = tornado.httpserver.HTTPServer(TheShieldsWebSocket)
server.listen(8085,'0.0.0.0')
tornado.ioloop.IOLoop.instance().start()



错误说:RuntimeError: There is no current event loop in thread 'Thread-1'

这到底是怎么回事?好像跟线程有关。

【问题讨论】:

  • 你的 Python 是哪个版本的?
  • 我使用 python 3.6。我听说async,但我不知道必须
  • 我可以在Python 2.x 上运行你的代码,还没有尝试Python 3.x
  • 你的龙卷风版本是多少?
  • '4.5.2',但我想这与龙卷风无关,这是threading 问题。

标签: python tornado


【解决方案1】:

假设你想写一个api,这个api可能需要等待一些东西才能渲染到客户端!这是实现这一目标的方法!正如您使用thread 一样,这很好,但不推荐。所以我们在 tornado 中调用了并发,你可以看到细节here

这里你的代码可以修改如下:

import tornado.httpserver
import tornado.ioloop
import tornado.web,tornado.websocket


class ClientWebSocketConnectionHandler(tornado.websocket.WebSocketHandler):
    def open(self):
            print ("opened")
    def on_message(self, message):
            self.write_message('test')    

class MainApplication(tornado.web.Application):
    def __init__(self):
        handlers = [(r'/User', ClientWebSocketConnectionHandler),]
        tornado.web.Application.__init__(self, handlers,)

TheShieldsWebSocket = MainApplication()
server = tornado.httpserver.HTTPServer(TheShieldsWebSocket)
server.listen(8085,'0.0.0.0')
tornado.ioloop.IOLoop.instance().start()

典型情况下,您会收到403 错误,在这种情况下您可以添加函数名称为check_origin

    # rest of above code
    def check_origin(self, param):
            return True

    def open(self):
            print ("opened")
    def on_message(self, message):
            self.write_message('test') 

如果你希望你的服务器可以处理多连接,你可以使用 `concurrent 方式来做到这一点,如下所示:

    @gen.coroutine
    def on_message(self, message):
            http_client = AsyncHTTPClient()
            response = yield http_client.fetch("http://example.com")
            do_something_with_response(response)
            self.write_message('test') 

【讨论】:

  • 我代码中的那个线程做了一些特别的事情,我需要它。因为它是向客户端发送数据的线程。
  • 相信你也可以通过这种方式做任何事情
  • @alexdayvi 首先,您必须首先意识到该线程只是一种无需阻塞主线程即可执行某些操作的方式,因此要发出信号或向您的客户端发送某些内容,只需在主线程上执行此操作即可把任务交给你的主人,你必须在异步上运行它。你怎么做?只需使用并发来做,这比你的线程更好。
  • 线程中正在处理某些事情,当涉及到特定结果时,必须将结果发送到线程内(由)的客户端。假设线程正在检查时间是否为 5:00,它会向所有用户发送一些消息。你说我必须在异步模式下运行thr
  • @alexdayvi 任何处理都可以打包成一个函数
猜你喜欢
  • 2021-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多