From 068d5d5bb4e37e88ee4828e8cd9fd0e4e05e1445 Mon Sep 17 00:00:00 2001 From: Yves Date: Sat, 13 Mar 2010 12:44:28 +0100 Subject: use heap instead of dict for storing blocks --- proxy.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/proxy.py b/proxy.py index 3249d2f..2522abd 100755 --- a/proxy.py +++ b/proxy.py @@ -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 -- cgit v1.2.1