0

I have the following problem. Whenever a child thread wants to perform some IO operation (writing to file, downloading a file) the program hangs. In the following example the program hangs on opener.retrieve. If I execute python main.py the program is blocked on an retrieve function. If I execute python ./src/tmp.py everything is fine. I don't understand why. Can anybody explain me what is happening?

I am using python2.7 on Linux system (kernel 3.5.0-27).

File ordering:

main.py
./src
    __init__.py
    tmp.py

main.py

import src.tmp

tmp.py

import threading
import urllib

class DownloaderThread(threading.Thread):
    def __init__(self, pool_sema, i):
        threading.Thread.__init__(self)
        self.pool_sema  = pool_sema
        self.daemon     = True
        self.i = i

    def run(self):
        try:
            opener = urllib.FancyURLopener({}) 
            opener.retrieve("http://www.greenteapress.com/thinkpython/thinkCSpy.pdf", "/tmp/" + str(self.i) + ".pdf")
        finally:
            self.pool_sema.release()

class Downloader(object):
    def __init__(self):
        maxthreads             = 1
        self.pool_sema         = threading.BoundedSemaphore(value=maxthreads)

    def download_folder(self):
        for i in xrange(20):
            self.pool_sema.acquire()
            print "Downloading", i
            t = DownloaderThread(self.pool_sema,i)
            t.start()

d = Downloader()
d.download_folder()
4

1 回答 1

0

我设法通过破解让它工作urllib.py——如果你检查它,你会看到许多import语句分散在代码中——即它“即时”使用导入内容,而不仅仅是在模块加载时。

所以,真正的原因仍然未知——但不值得调查——可能是 Python 的导入系统中的一些死锁。您只是不应该在运行期间运行重要的代码import- 那只是自找麻烦。

如果你坚持,如果你将所有这些奇怪的导入语句移到 urllib.py 的开头,你就可以让它工作。

于 2013-04-26T23:20:19.597 回答