首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用线程的Tornado WebSockets非阻塞请求。我做得对吗?

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

Stack Overflow用户
提问于 2018-06-01 05:56:42
回答 1查看 713关注 0票数 2

我是Python的新手,我试图解决的问题是有一个正在进行的websocket连接,它可以接受请求,在执行calc时等待一段时间,在准备好时返回结果,同时不会阻塞来自其他用户/客户端/连接的其他请求。我已经做到了这一点,它的工作相当不错,但想检查这是不是正确的解决方案。在这个特定的代码片段中,它们的关键部分是在打开时执行的操作:我关闭了另一个休眠线程。

代码语言:javascript
复制
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连续返回其他输出(滴答)时,我单击按钮时:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2018-06-01 09:36:16

是的,我看这个不错。要记住的关键一点是,在worker_A (传递给executor.submit()的函数)中,不能调用write_message。相反,返回一个值并在yield executor.submit()返回后写入它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50633359

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档