问题描述
我正在按照一些示例代码使用 asyncore
此处,只需为 asyncore.loop
设置一个 timeout
值,如下面的完整示例所示:
I am following some example code to use asyncore
here, only having set a timeout
value for asyncore.loop
as in the following full example:
import smtpd
import asyncore
class CustomSMTPServer(smtpd.SMTPServer):
def process_message(self, peer, mailfrom, rcpttos, data):
print 'Receiving message from:', peer
print 'Message addressed from:', mailfrom
print 'Message addressed to :', rcpttos
print 'Message length :', len(data)
return
server = CustomSMTPServer(('127.0.0.1', 1025), None)
asyncore.loop(timeout = 1)
我预计 1 秒后会发生超时,但事实并非如此.代码运行时间超过一秒钟.我在这里错过了什么?
I have expected that a timeout occurs after 1 second, but this is not the case. The code runs much longer for than one second. What am I missing here?
推荐答案
我真的不知道 asyncore.loop()
的 timeout
参数是否真的是为了在指定时间后超时函数调用 asyncore.loop()
,但这里是一个收据,使该函数在指定时间后超时(用 asyncore.loop()
替换该行code> 在示例代码中):
I really do not know if the timeout
argument to asyncore.loop()
really is meant to timeout the function call asyncore.loop()
after the specified time, but here is a receipt to make that function timeout after a specified time (replacing the line with asyncore.loop()
in the example code):
import signal
class TimeoutError(Exception): pass
# define the timeout handler
def handler(signum, frame):
raise TimeoutError()
# set the timeout handler and the signal duration
signal.signal(signal.SIGALRM, handler)
signal.alarm(1)
try:
asyncore.loop()
except TimeoutError as exc:
print "timeout"
finally:
signal.alarm(0)
这篇关于当没有更多连接时 asyncore.loop 不会终止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!