tornado.tcpserver.TCPServer.stop
的文档说:
Requests currently in progress may still continue after the server is stopped
但是,为了测试稳健性,我想模拟服务器的完全崩溃。
有没有一种首选方法可以突然终止tornado.TCPServer?特别是,我想不优雅地终止所有打开的套接字。
编辑
所以我认为 stop
不会完全终止现有连接。以下代码打印 1, 2
from tornado import gen
from tornado.ioloop import IOLoop
from tornado.tcpserver import TCPServer
from tornado.tcpclient import TCPClient
port = 9000
class Server(TCPServer):
@gen.coroutine
def handle_stream(self, stream, address):
while True:
bytes = yield stream.read_bytes(5)
yield stream.write(bytes)
server = Server()
server.listen(port)
@gen.coroutine
def client():
client = TCPClient()
stream = yield client.connect('127.0.0.1', port)
yield stream.write(b'Hello')
result = yield stream.read_bytes(5)
assert result == b'Hello'
print(1)
server.stop()
yield stream.write(b'Hello')
result = yield stream.read_bytes(5)
assert result == b'Hello'
print(2)
IOLoop.current().run_sync(client)
最佳答案
TCPServer.stop
正是您想要的;您引用的评论是一个警告,表明 TCPServer
不会尝试正常关闭。
关于python - 停止tornado.TCPServer而不等待连接关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32917436/