diff options
author | Yves <yvesf-git@xapek.org> | 2010-03-13 12:44:28 +0100 |
---|---|---|
committer | Yves <yvesf-git@xapek.org> | 2010-03-13 12:44:28 +0100 |
commit | 068d5d5bb4e37e88ee4828e8cd9fd0e4e05e1445 (patch) | |
tree | 366bd87c82ac2833bf45bd919f03a7a7ea085c4d /proxy.py | |
parent | 28259e5cf5cb5bfbba2ef054ebe7b94096175b68 (diff) | |
download | magicproxy-068d5d5bb4e37e88ee4828e8cd9fd0e4e05e1445.tar.gz magicproxy-068d5d5bb4e37e88ee4828e8cd9fd0e4e05e1445.zip |
use heap instead of dict for storing blocks
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 |