首页 > 解决方案 > 使用 Python 龙卷风处理多个传入请求

问题描述

我有一个代码可以很好地满足一个用户请求。但是,它不能处理多个请求,它等待一个请求完成,然后处理第二个请求。如何同时处理多个请求。

import tornado.ioloop
import tornado.web
import tornado.websocket
from tornado import gen
import random
import time
import sys

这是一个模拟一些传入数据的类。

class Message():


    def __init__(self):
        self.dct = {'foo': 0, 'bar': 0, 'durrr': 0}
        self.keys = list(self.dct.keys())


    def update_key(self):
        dct_key = random.choice(self.keys)
        ran = random.choice(range(10, 21))
        self.dct[dct_key] += ran
        if self.dct[dct_key] >= 100:
            self.dct[dct_key] = 'Loading Completed'
            self.keys.remove(dct_key)


    def is_completed(self):
        return True if self.keys == [] else False


    def __str__(self):
        strng = ''
        for key, value in self.dct.items():
            if type(value) == int:
                strng += '{}: {}% completed.<br>'.format(key, value)
            else:
                strng += '{}: {}<br>'.format(key, value)
        return strng

此类通过套接字发送数据。

class EchoWebSocket(tornado.websocket.WebSocketHandler):


    def open(self):
        print("WebSocket opened")


    def on_message(self, message):
        msg = Message()
        while not msg.is_completed():
            msg.update_key()
            try:
                fut = self.write_message('Download progress for user xyz:<br>{}Download in progress! Please wait...<br>'.format(msg.__str__()))
                print(fut)
            except tornado.websocket.WebSocketClosedError:
                print('WebSocket is closed')
                break
            time.sleep(1)
        self.write_message('Download progress for user xyz:<br>{}Download completed. You may proceed.'.format(msg.__str__()))
        #sys.exit('Program terminated.')


    def on_close(self):
        print("WebSocket closed")

主类很简单。呈现一些 html。主引擎在 EchoWebSocket 中。

class Main(tornado.web.RequestHandler):


    def get(self):
        self.render('counter2.html', title='Websockets')


application = tornado.web.Application([
    (r"/", Main),
    (r"/websocket", EchoWebSocket),
])


if __name__ == "__main__":
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

和html:

<!doctype html>
<html>
   <head>
       <title>Tornado Test</title>
       <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js"></script>
    </head>
    <body>
        <script>    
            $(document).ready(function () {
                var ws = new WebSocket("ws://localhost:8888/websocket");
                ws.onopen = function() {
                    ws.send("Hello, world");
                };
                ws.onmessage = function (evt) {
                    document.getElementById("myDIV").innerHTML = evt.data + "<br>";
                };
            });
        </script>
        <div id="myDIV"></div>
  </body>
</html>

标签: pythontornado

解决方案


你正在使用time.sleep(1). 但是time.sleep是一个阻塞功能,这意味着它将停止整个服务器,并且在此期间没有其他任何东西能够运行。

FAQsTornado页面中也提到了这一点。

您需要的是异步睡眠功能。龙卷风有gen.sleep。像这样使用它:

async def my_func():
    ...
    await gen.sleep(1)

推荐阅读