Почему в этом коде при эксепшне в Worker::run() в pic щлазывается Null? Если файл скачивается, то всё ок, т. е. в секции try: всё работает нормально, но ошибку залогировать не могу, ибо в секции except: всегда None…
import sys, threading, time, csv
from datetime import datetime
import urllib
threads_num = int(sys.argv[1])
urls_file = sys.argv[2]
output_path = sys.argv [3]
num = 0
class Worker(threading.Thread):
def __init__(self, pic_list, output_lock):
super(Worker, self).__init__()
self.pic_list = pic_list
self.output_lock = output_lock
def run(self):
for pic in self.pic_list[:-1]:
try:
urllib.urlretrieve(pic[2], output_path + pic[0] + ' - ' + pic[1] + '.gif')
except IOError as exp:
self.output_lock.acquire()
log('Error with url: ' + pic[2], self)
print 'Error with url: ' + pic[2], self
self.output_lock.release()
log_file = open('log.txt', 'w')
def log(message, obj):
log_file.write('%s:%03d\t%s\t%s\n' % (datetime.today().strftime('%Y-%m-%d %H:%M:%S'), datetime.today().microsecond / 1000, obj.name, message))
reader = csv.reader(open(urls_file, 'rb'))
ls = []
for row in reader:
ls.append(row)
chunks = [ls[i::threads_num] for i in range(threads_num)]
output_lock = threading.Lock()
workerlist = []
for chunk in chunks:
new_worker = Worker(chunk, output_lock)
workerlist.append(new_worker)
new_worker.start()