1
# $Id: http_manager.py 270 2004-10-09 10:38:54Z fredrik $
4
# manage a set of http clients
6
# Copyright (c) 2001-2004 by Fredrik Lundh. All rights reserved.
9
from __future__ import absolute_import
23
def request(self, uri, consumer, extra_headers=None):
24
self.queue.append((uri, consumer, extra_headers))
26
def priority_request(self, uri, consumer, extra_headers=None):
27
self.queue.insert(0, (uri, consumer, extra_headers))
30
for channel in asyncore.socket_map.values():
34
def prioritize(self, priority_uri):
36
for uri, consumer, extra_headers in self.queue:
37
if uri == priority_uri:
39
self.priority_request(uri, consumer, extra_headers)
43
def poll(self, timeout=0.1):
46
for channel in asyncore.socket_map.values():
47
if channel.bytes_in > self.max_size:
48
channel.close() # too much data
50
channel.consumer.http(
51
0, channel, ("HTTPManager", "too much data", None)
55
if channel.timestamp and now - channel.timestamp > self.max_time:
56
channel.close() # too slow
58
channel.consumer.http(
59
0, channel, ("HTTPManager", "timeout", None)
63
# activate up to max_connections channels
64
while self.queue and len(asyncore.socket_map) < self.max_connections:
65
http_client.do_request(*self.queue.pop(0))
66
# keep the network running
67
asyncore.poll(timeout)
68
# return non-zero if we should keep on polling
69
return len(self.queue) or len(asyncore.socket_map)