Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
braindead downloader
import threading, sys
import urlparse, httplib
is_done = False
class Downloader(threading.Thread):
def __init__(self, url, buf, n, semin, semout):
self.buf = buf
self.bufn = n
self.semin = semin
self.semout = semout
scheme, netloc, path, query, fragment = urlparse.urlsplit(url)
self.host, self.path = netloc, path
threading.Thread.__init__(self)
def run(self):
h = httplib.HTTPConnection(self.host)
h.request("GET", self.path)
r = h.getresponse()
global is_done
i = 0
while True:
self.semin.acquire()
self.buf[i] = r.read(1024)
## when to stop???
if not self.buf[i]:
is_done = True
break
i = (i+1) % self.bufn
self.semout.release()
h.close()
class Writer(threading.Thread):
def __init__(self, dest, buf, n, semin, semout):
self.dest = dest
self.buf = buf
self.bufn = n
self.semin = semin
self.semout = semout
threading.Thread.__init__(self)
def run(self):
global is_done
i = 0
f = open(self.dest, "w")
while True:
if is_done:
break
self.semout.acquire()
f.write(buf[i])
i = (i+1) % self.bufn
self.semin.release()
f.close()
n = 100
buf = [0 for i in range(n)]
semin = threading.Semaphore(n)
semout = threading.Semaphore(0)
url="http://www.google.com"
dest = "tmp"
if len(sys.argv) > 1:
url = sys.argv[1]
if len(sys.argv) > 2:
dest = sys.argv[2]
dl = Downloader(url=url, buf=buf, n=n, semin=semin, semout=semout)
wr = Writer(dest=dest, buf=buf, n=n, semin=semin, semout=semout)
dl.start()
wr.start()
@ifduyue

This comment has been minimized.

Copy link

@ifduyue ifduyue commented Jul 10, 2012

buf = [0 for i in range(n)] => buf = [0] * n

@zhangsen

This comment has been minimized.

Copy link
Owner Author

@zhangsen zhangsen commented Jul 10, 2012

@lyxint, 差不多吧,写过就扔的代码,不需要搞微优化。

主要问题是如何判断已经下载完成。这个 is_done 全局变量会有 race condition。

@ifduyue

This comment has been minimized.

Copy link

@ifduyue ifduyue commented Jul 10, 2012

最好别用0, 用None可以省空间

@zhangsen

This comment has been minimized.

Copy link
Owner Author

@zhangsen zhangsen commented Jul 10, 2012

@lyxint 万恶之源,万恶之源啊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.