简体   繁体   English

python龙卷风websocket错误:RuntimeError:线程'Thread-1'中没有当前事件循环

[英]python tornado websocket error: RuntimeError: There is no current event loop in thread 'Thread-1'


i have the following code, it run well on Tornado <5 but it run into failure if i run it by Tornado>= 5, 我有以下代码,它在Tornado <5上运行良好,但如果我通过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()



Error says: RuntimeError: There is no current event loop in thread 'Thread-1' 错误消息: RuntimeError: There is no current event loop in thread 'Thread-1'

what the hell wrong with? 到底是怎么了? it looks like it has something to do with the thread. 看起来与线程有关。

Assume you want write a api, this api may be need to wait something before render to the client side! 假设您要编写一个api,则可能需要先等待该api才能呈现给客户端! here are serval way to achieve that! 这是实现这一目标的有用方法! as you have use thread and this is good but no recommend. 因为您有使用thread ,这很好,但不建议使用。 so we have invoke concurrent in tornado, you can see detail here . 因此,我们在龙卷风中调用并发,您可以在此处查看详细信息

And here you code can be modify as below: 在这里,您可以对代码进行如下修改:

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()

Typical, you will got a 403 error, in that situation you can add a function name as check_origin . 通常,您会收到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') 

And then if you want you server can handler multi connection, you can use `concurrent way to do that like below : 然后,如果您希望服务器可以处理多重连接,则可以使用并发方式执行以下操作:

    @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') 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 RuntimeError: 线程 &#39;Thread-1&#39; 中没有当前事件循环,多线程和异步错误 - RuntimeError: There is no current event loop in thread 'Thread-1' , multithreading and asyncio error 多线程 python 时的运行时错误“运行时错误:线程 &#39;Thread-1&#39; 中没有当前事件循环。” - RuntimeError when multithreading python "RuntimeError: There is no current event loop in thread 'Thread-1'." /accounts/register/ 处的 RuntimeError 线程 'Thread-1' 中没有当前事件循环 - RuntimeError at /accounts/register/ There is no current event loop in thread 'Thread-1' 我不知道如何解决以下错误; RuntimeError:线程“Thread-1”和“Thread-2”中没有当前事件循环 - I don't know how to fix the following error; RuntimeError: There is no current event loop in thread 'Thread-1' and 'Thread-2' 线程 'Thread-1' 中没有当前事件循环 - There is no current event loop in thread 'Thread-1' RuntimeError:线程“ Thread-1”中没有当前事件循环。 -request_html,html.render() - RuntimeError: There is no current event loop in thread 'Thread-1'. - requests_html, html.render() requests-html“RuntimeError:在 flask 端点上使用时,线程‘Thread-1’中没有当前事件循环 - requests-html "RuntimeError: There is no current event loop in thread 'Thread-1' when using it on a flask endpoint 将 Telethon 与 django 一起使用:线程 'Thread-1' 中没有当前事件循环 - Using telethon with django: There is no current event loop in thread 'Thread-1' RuntimeError:线程&#39;Dummy-1&#39;中没有当前事件循环 - RuntimeError: There is no current event loop in thread 'Dummy-1' RuntimeError: There is no current event loop in thread … DiscordPy MultiThreading - RuntimeError: There is no current event loop in thread … DiscordPy MultiThreading
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM