使用一个简单的select循环可以很好地工作。下面是要完成的代码片段:def run(self, resolvers_iter):
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0)
sock.setblocking(False)
try:
pending = []
# For rate limiting
limit = float(self.timeout)/self.max_pending # delay between sends
last_sent = clock() - limit
# Work as long as there are more resolvers to query
completed_sending = False
while not completed_sending or pending:
# Can I send more requests
want_to_write = False
if not completed_sending and len(pending) < self.max_pending:
want_to_write = True
# Calculate nearest timeout time to make sure select returns on time
timeout = None
if pending:
timeout = self.timeout - clock() + pending[0][0] + 0.001
timeout = max(timeout, 0)
# Rate limit