[英]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.