因此,我用python编写了一个HTML-NLP,它使用多处理库来加快处理速度,但我遇到的问题是,我需要访问一个全局变量,作为计数器,这样每次文件更改时我都可以重置它。。。现在问题有两个方面。。。一个我不能在不影响计数的情况下影响这个全局计数器,而另一个问题是我不能使它成为一个实例变量,因为这个数需要比实例持续的时间长。。。因此,任何帮助将不胜感激。我想我可以通过回调来更改全局的值,但这将使我处于与以前相同的位置,因为它凌驾于其他工人所拥有的值之上。在
我的目标是重写paragraph_count,以便在解析每个文件grope之后将其重置为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
另外,我知道你们中的一个会告诉我全局变量是个坏主意。。我也同意。但在这种情况下,我没有看到一个好的选择,这将是这个问题的主要原因。在
你不能有一个全局变量,但不是全局变量,这实际上就是你所要求的。如果没有
multiprocessing
.*所以,你需要以其他方式通过它。例如,您可以在每个任务使用的顶级函数中使其成为本地的,或者找到一些其他对象,该对象的作用时间与整个任务一样长,并使其成为该对象的成员,或者将其作为参数传递,或者传递一个列表,其中包含一个值作为参数(这允许您通过设置
lst[0] = new_value
来改变值),或者在您的代码中,实际上根本没有在
worker
中的while
循环之外使用word_list
,因此……它绝对没有理由是全局的。然而,在实际的代码中可能不是这样。如果您向我们展示您的真实代码(或者,最好是一个SSCCE),它演示了在没有所有无关内容的情况下您要做什么,我们可以解释如何做您想要的。否则,我们只能给出模糊的解释。在*实际上,
multiprocessing
在这里确实有区别。您的代码依赖于这样一个事实:由于在fork
之上进行了多处理,并且解释器以您希望的方式处理,所以全局变量是真正全局的。这并不能保证在Windows上运行,每个进程都将获得一个单独的副本(这意味着池中同一进程运行的所有任务将彼此共享一个副本,但不能与池中其他进程运行的任务共享一个副本),而且在一些不常见的POSIX平台上,您只需执行segfault。在相关问题 更多 >
编程相关推荐