diff options
Diffstat (limited to 'proxy.py')
-rwxr-xr-x | proxy.py | 18 |
1 files changed, 10 insertions, 8 deletions
@@ -1,6 +1,7 @@ #!/usr/bin/python -t import os, sys, string, time import asynchat, asyncore, socket, httplib, urlparse +from heapq import heappush, heappop try: import cStringIO as StringIO except ImportError: @@ -106,7 +107,7 @@ class MultipleProxyReader(object): self.fetch_pos = 0 self.write_pos = 0 self.buffer = "" - self.blocks = dict() + self.blocks = list() self.fetchers = list() for i in range(len(ENDPOINTS)): @@ -116,7 +117,7 @@ class MultipleProxyReader(object): if len(data) == 0: self.fetchers = filter(lambda f: f != fetcher, self.fetchers) else: - self.blocks[fetcher.pos] = data + heappush(self.blocks, (fetcher.pos, data)) if fetcher.range[1] - fetcher.pos < FETCHER_JUMPSTART \ and self.fetch_pos + 1 < self.content_length and not self.channel.is_closed \ @@ -158,14 +159,15 @@ class MultipleProxyReader(object): return False #print self, "expect data at %s in" % self.write_pos, self.blocks.keys() - if self.write_pos in self.blocks.keys(): - data = self.blocks.pop(self.write_pos) - self.channel.push(data) - self.write_pos += len(data) + if len(self.blocks)>0 and min(self.blocks)[0] == self.write_pos: + item = heappop(self.blocks) + self.channel.push(item[1]) + self.write_pos += len(item[1]) return True - + if self.write_pos + 1 >= self.content_length: - print self, "job done" + print self, "job done %s blocks left" % len(self.blocks) + #XXX prevent next calls to send_next_data self.channel.close_when_done() return False |