我怎样才能使一个合作进程停止超时?在
我不明白为什么asyncio.wait()对我不起作用。
我有这样一段代码(计划实现telnet客户端):def expect(self, pattern, timeout=20):
if type(pattern) == str:
pattern = pattern.encode('ascii', 'ignore')
return self.loop.run_until_complete(asyncio.wait_for(self.asyncxpect(pattern), timeout))
async def asyncxpect(self, pattern): #receives data in a cumulative way until match is found
regexp = re.compile(b'(?P[\s\S]*)(?P%s)' %pattern)
self.buffer = b''
while True:
# add timeout
# add exception handling for unexpectedly closed connections
data = await self.loop.sock_recv(self.sock, 10000)
self.buffer += data
m = re.match(regexp, self.buffer)
if m:
payload = m.group('payload')
match = m.group('pattern')
return payload, match
正如我所想的,在某个时刻(在await语句中)将控制权返回给事件循环。我认为应该在没有更多数据可接收时发生。
如果事件循环有控制权,它可以停止超时。在
但若服务器并没有发送任何有用的(匹配的)代码,我的代码就会在这个循环中出错,就在等待点。在
我认为它与这个问题Python asyncio force timeout不同,因为我没有使用像时间。睡觉(n) 一。在