3xi*_*ian 6 python sockets tornado
如何为龙卷风IOStream设置超时?
我尝试通过以下方式构建IOStream:
sock = socket.socket()
sock.settimeout(5)
self.stream = tornado.iostream.IOStream(sock)
Run Code Online (Sandbox Code Playgroud)
但是当我打电话时stream.read_bytes(),它仍然会一直等待.
你不能使用socket.settimeout(),因为它是为阻塞 IO 而设计的,而 Tornado 提供非阻塞 IO。
Tornado 高度面向 Web 和 HTTP IO,它不允许在没有极度痛苦的情况下进行低级网络编程(来源IOStream令人恐惧)。
在套接字上设置超时的最佳方法是使用select.select()、select.poll()等,但将这种方法与 Tornado 集成起来很痛苦。
我已经成功地使用结合gen.with_timeout和脏黑客来清除流状态来执行超时读取。
from tornado import gen
from tornado.ioloop import IOLoop
from tornado.tcpclient import TCPClient
timeout = 5
io_loop = IOLoop.current()
factory = TCPClient(io_loop=io_loop)
@gen.coroutine
def run():
stream = yield factory.connect('127.0.0.1', 1234)
try:
future = stream.read_bytes(128)
data = yield gen.with_timeout(
timeout=io_loop.time() + timeout,
future=future,
io_loop=io_loop,
)
except gen.TimeoutError:
# A dirty hack to cancel reading and to clear state of the stream, so
# stream will be available for reading in future
io_loop.remove_handler(stream.socket)
state = (stream._state & ~io_loop.READ)
stream._state = None
stream._read_callback = None
stream._read_future = None
stream._add_io_state(state)
Run Code Online (Sandbox Code Playgroud)
祝你好运!
| 归档时间: |
|
| 查看次数: |
1169 次 |
| 最近记录: |