python - 使用 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>
解决方案
你正在使用time.sleep(1)
. 但是time.sleep
是一个阻塞功能,这意味着它将停止整个服务器,并且在此期间没有其他任何东西能够运行。
FAQs
Tornado页面中也提到了这一点。
您需要的是异步睡眠功能。龙卷风有gen.sleep
。像这样使用它:
async def my_func():
...
await gen.sleep(1)
推荐阅读
- c# - “内容”属性被多次定义
- python - 绘制没有组的箱线图 seaborn
- python - 在 Python 中将数据从 CSV 文件加载到 MySQL 数据库表时出错
- flutter - 如何使用flutter/dart反序列化带有地图列表的firestore文档?
- mysql - 随时随地在 RPA 自动化中连接数据库
- python - 使用 matplotlib 轮廓时出现 Pyomo TypeError
- unity3d - 在没有空间映射的情况下在现实世界中定位对象的其他选项;Holo Lens 1 / 混合现实工具包
- django - django-background-tasks 不适用于 django-hosts
- spring-boot - org.springframework.boot 的 PKIX 路径构建失败
- python - 如何在pyqt5中将QAction添加到QLineEdit?