Python Tornado WebSocket使用线程处理非阻塞请求。我做得对吗?
我是Python新手,我试图解决的问题是,有一个持续的websocket连接,可以接收请求,在执行Calc时等待一段时间,准备好后返回结果,同时不阻止来自其他用户/客户端/连接的其他请求。我已经做到了这一点,它的工作相当不错,但想检查一下这是否是正确的解决方案。在这个特殊的片段中,他们的关键点是打开时执行的操作:我用睡眠来剥离另一个线程Python Tornado WebSocket使用线程处理非阻塞请求。我做得对吗?,python,websocket,tornado,Python,Websocket,Tornado,我是Python新手,我试图解决的问题是,有一个持续的websocket连接,可以接收请求,在执行Calc时等待一段时间,准备好后返回结果,同时不阻止来自其他用户/客户端/连接的其他请求。我已经做到了这一点,它的工作相当不错,但想检查一下这是否是正确的解决方案。在这个特殊的片段中,他们的关键点是打开时执行的操作:我用睡眠来剥离另一个线程 import tornado.web import tornado.websocket import tornado.httpserver import tor
import tornado.web
import tornado.websocket
import tornado.httpserver
import tornado.ioloop
import time
import json
from tornado import gen
from concurrent.futures import ThreadPoolExecutor
from tornado.options import define, options, parse_command_line
define("port", default=8888, type=int)
thread_pool = ThreadPoolExecutor(2)
class WebSocketHandler(tornado.websocket.WebSocketHandler):
# Make this an asynchronous coroutine
@gen.coroutine
def on_message_coroutine(self, message):
self.write_message('Message:', message)
def worker_A(websocket, x):
time.sleep(1)
print('TICK', x)
pass
return x
print('scheduling and yielding')
for x in range(0, 30):
test = yield thread_pool.submit(worker_A, self, x)
self.write_message(json.dumps(test))
print('done yielding')
def open(self, *args):
print("New connection")
tornado.ioloop.IOLoop.current().spawn_callback(self.on_message_coroutine, 'New Msg')
def check_origin(self, origin):
return True
def on_message(self, message):
print("New message {}".format(message))
self.write_message("You sent me this, sending it back in upper: " + message.upper())
def on_close(self):
print("Connection closed")
app = tornado.web.Application([
(r'/ws/', WebSocketHandler),
])
if __name__ == '__main__':
app.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
下面是前端控制台上的输出。“新消息”位是指当websocket连续返回其他输出(滴答声)时,我单击按钮:
是的,我觉得这个不错。要记住的关键是,在
worker\u A
(传递给executor.submit()
的函数中,不能调用write\u message
。相反,返回一个值并在产生executor.submit()后写入已经返回。看起来不错,但可能考虑使用<代码>异步/等待< /COD>而不是<代码>屈服>代码。谢谢。实际上,在我找到这个解决方案之前,这就是我的问题。在生成器中,它不知道自己或甚至通过WebSoCK对象。它只是某种程度上失去了范围。你知道确切的原因吗?对不起,我不知道。我不理解此代码的其他版本可能在做什么。如果您遇到其他问题,请提出一个新问题并提供更多详细信息。
app.component.ts:17 Response from websocket: 11
app.component.ts:17 Response from websocket: 12
app.component.ts:17 Response from websocket: 13
app.component.ts:17 Response from websocket: 14
app.component.ts:24 new message from client to websocket: gotta be a string
app.component.ts:17 Response from websocket: You sent me this, sending it back in upper: "GOTTA BE A STRING"
app.component.ts:17 Response from websocket: 15
app.component.ts:17 Response from websocket: 16
app.component.ts:17 Response from websocket: 17
app.component.ts:24 new message from client to websocket: gotta be a string
app.component.ts:17 Response from websocket: You sent me this, sending it back in upper: "GOTTA BE A STRING"
app.component.ts:17 Response from websocket: 18
app.component.ts:17 Response from websocket: 19
app.component.ts:17 Response from websocket: 20
app.component.ts:17 Response from websocket: 21