[英]Multiprocessing in Python <urlopen error ftp error> when Downloading Bataches of Files
[英]Multiprocessing Error when Downloading files from FTP
一段時間以來,我一直沉迷於代碼的特殊之處,並且可以弄清楚為什么它無法正常工作。 當我運行下面的代碼時,我會收到一個酸洗錯誤,並且總是在另一個文件中。
這將下載隨機數量的文件,然后神奇地停止。 由於某種原因,第ith個文件名(或它選擇停止的任何位置)突然無法腌制,而之前的其他文件卻可以腌制。 我可以看到他們都沒有被與pickle或沒有人,但具有任意數量的不與pickle僅僅是怪異。
n=10
urls = ["ftp://ftp.sec.gov/{0:s}".format(f) for f in flist[:n]]
print urls
from multiprocessing import Pool
from urllib import urlretrieve
def download(url):
try:
file_name = str(url.split('/')[-1])
print file_name
return urlretrieve(url, file_name), None
except Exception as e:
return None, e
if __name__ == "__main__":
p = Pool(10)
p.map(download, urls)
我得到的錯誤是:
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
File "/usr/local/lib64/anaconda/lib/python2.7/multiprocessing/pool.py", line 250, in map
return self.map_async(func, iterable, chunksize).get()
File "/usr/local/lib64/anaconda/lib/python2.7/multiprocessing/pool.py", line 554, in get
raise self._value
multiprocessing.pool.MaybeEncodingError: Error sending result: '[(('0000950144-94-000788.txt', <mimetools.Message instance at 0x6333878>), None)]'. Reason: 'PicklingError("Can't pickle <type 'cStringIO.StringI'>: attribute lookup cStringIO.StringI failed",)'
有誰知道為什么ith元素突然無法腌制? 而其他人以前呢? 我可以看到這是因為它認為ith名稱是stringIO,但這沒有任何意義,因為之前的名稱也應該如此。
您將返回一個代表FTP標頭的mimetools.Message對象。 換個小節,或者在返回它之前將其轉換為普通字符串。
這是后者的一個示例:
n=10
urls = ["ftp://ftp.sec.gov/{0:s}".format(f) for f in flist[:n]]
print urls
from multiprocessing import Pool
from urllib import urlretrieve
def download(url):
try:
file_name = str(url.split('/')[-1])
print file_name
filename,headers = urlretrieve(url, file_name)
return (filename, repr(headers), None
except Exception as e:
return None, e
if __name__ == "__main__":
p = Pool(10)
p.map(download, urls)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.