だから私はPythonでマルチプロセッシングライブラリを使用して物事をより速くするHTML NLPのようなものを書いたが、私が遭遇した問題は、カウンターとして使用するグローバルにアクセスする必要があるということだ。ファイルが変更されるたびにリセットします...問題は2つあります... 1つは、私がやりたいことではない他の所有者とのカウントに影響を与えずにこのグローバルカウンターに影響を与えることはできません。もう1つの問題はインスタンスが持続するよりも数が長く持続する必要があるため、これをインスタンス変数にすることはできません...そのため、どんな助けも大歓迎です。コールバックを使用してグローバルの値を変更できると考えていましたが、それでは以前と同じ立場になり、他のワーカーが所有する値を上書きしてしまいます。
私の目標は、paragraph_count をオーバーライドして、各ファイルの手探りが解析された後に 0 にリセットされるようにすることです。
ここに私のマルチプロセッシングコードがあります:
def worker(word_output_path, html_output_path, source, filename):
if filename:
t = HTML(source)
output = open(html_output_path, 'w')
word_out = open(word_output_path,'w')
with output, word_out:
try:
output.write(t.tokenized)
global word_list
for w in word_list:
if w:
word_out.write(w+'\n')
word_list = []
except IndexError:
output.write(s[1])
except UnboundLocalError:
output.write(s[1])
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):
extHTML = ['.html', '.xhtml', '.htm', '.xml']
pool = mp.Pool()
for root, dirs, files in os.walk(self.input_path):
epubs = [os.path.join(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]
for count, e in enumerate(epubs):
epub = epubLoader(e)
print os.path.splitext(os.path.basename(e))[0]
for filename, ext, source, file, epub in epub.get_html_from_epub():
if ext in extHTML:
html_output_path = os.path.normpath(output_file[count]+os.path.dirname(file)+'/'+filename+ext)
word_output_path = os.path.normpath(output_file[count]+os.path.dirname(file)+'/'+filename+'.txt')
self.ensure_dir(os.path.dirname(html_output_path))
pool.apply_async(
worker,
args=(os.path.normpath(word_output_path), os.path.normpath(html_output_path), source, filename), callback = self.update_value)
# this is where I will output the other files.
else:
output_path = os.path.normpath(output_file[count]+os.path.dirname(file)+'/'+filename+ext)
epub.extract(file, os.path.normpath(output_file[count]))
pool.close()
pool.join()
def update_value(self):
paragraph_count = 0
補足として、あなたの 1 人がグローバル変数は悪い考えだと言うことを知っています..そして私は同意します。しかし、この場合、私は良い代替手段を見つけられませんでした。それがこの質問の主な理由です.