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

时间:2022-04-10 01:08:10


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'

what the hell wrong with? it looks like it has something to do with the thread.

错误说:RuntimeError:线程'Thread-1'中没有当前的事件循环到底是怎么回事?看起来它与线程有关。

1 个解决方案

#1


0  

Assume you want write a api, this api may be need to wait something before render to the client side! here are serval way to achieve that! as you have use thread and this is good but no recommend. so we have invoke concurrent in tornado, you can see detail here.

假设你想写一个api,这个api可能需要在渲染之前等待客户端!这里有seriv方式来实现这一点!因为你有使用线程,这是好的,但不建议。所以我们在龙卷风中调用并发,你可以在这里看到细节。

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

#1


0  

Assume you want write a api, this api may be need to wait something before render to the client side! here are serval way to achieve that! as you have use thread and this is good but no recommend. so we have invoke concurrent in tornado, you can see detail here.

假设你想写一个api,这个api可能需要在渲染之前等待客户端!这里有seriv方式来实现这一点!因为你有使用线程,这是好的,但不建议。所以我们在龙卷风中调用并发,你可以在这里看到细节。

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