Tornado WebSockets使用线程的非阻塞请求。我做得对吗?

时间:2018-05-31 21:56:42

标签: python websocket tornado

我是Python的新手,我正在尝试解决的问题是有一个正在进行的websocket连接,可以接收请求,等待一段时间进行计算,准备好后返回结果,同时不阻止其他请求来自其他用户/客户/连接。我已经实现了这个并且它的工作相当不错但是想要检查这是否是正确的解决方案。在这个特定的片段中,它们的关键位是在打开时执行的操作:我在休眠时关闭另一个线程。

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不断返回其他输出时(TICKS):

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

1 个答案:

答案 0 :(得分:0)

是的,这对我来说很好看。要记住的关键是在worker_A(您传递给executor.submit()的函数中,您不能调用write_message。而是返回一个值并在yield executor.submit()之后写入返回。