如何使多处理器系统工作,从而在列表中生成新作业?我不断得到:
断言 self._popen 是无,'不能启动一个进程两次' AttributeError:'Worker' 对象没有属性 '_popen'
这是有道理的,因为我基本上是在制作同一项工作的多个实例……那我该如何解决呢?我需要设置多处理器池吗?
让我知道是否需要进一步澄清。
这是我的多处理类:
class Worker(multiprocessing.Process):
def __init__(self, output_path, source, file_name):
self.output_path = output_path
self.source = source
self.file_name = file_name
def run(self):
t = HTML(self.source)
output = open(self.output_path+self.file_name+'.html','w')
word_out = open(self.output_path+self.file_name+'.txt','w')
try:
output.write(t.tokenized)
for w in word_list:
if w:
word_out.write(w+'\n')
word_out.close()
output.close()
word_list = []
except IndexError:
output.write(s[1])
output.close()
word_out.close()
except UnboundLocalError:
output.write(s[1])
output.close()
word_out.close()
这是实现整个事情的类。
class implement(HTML):
def __init__(self, input_path, output_path):
self.input_path = input_path
self.output_path = output_path
def ensure_dir(self, directory):
if not os.path.exists(directory):
os.makedirs(directory)
return directory
def prosses_epubs(self):
for root, dirs, files in os.walk(self.input_path+"\\"):
epubs = [root+file for file in files if file.endswith('.epub')]
output_file = [self.ensure_dir(self.output_path+"\\"+os.path.splitext(os.path.basename(e))[0]+'_output\\') for e in epubs]
count = 0
for e in epubs:
epub = epubLoader(e)
jobs = []
# this is what's breaking everything right here. I'm not sure how to fix it.
for output_epub in epub.get_html_from_epub():
worker = Worker(output_file[count], output_epub[1], output_epub[0])
jobs.append(worker)
worker.start()
for j in jobs:
j.join()
count += 1
print "done!"
if __name__ == '__main__':
test = implement('some local directory', 'some local directory')
test.prosses_epubs()
对此的任何帮助将不胜感激。也让我知道我在代码中所做的事情是否可以做得更好......我一直在努力学习如何以最好的方式做事。