我具有以下功能:
def print_hamming_distance(calls):
#calls is a dictionary
samples = calls.keys()
with Pool(8) as pool: #Parallel Process
for dist, sample1, sample2 in pool.imap(multi_proc_hamming_distance, itertools.combinations(samples,2)):
print( dist, sample1, sample2 )
def multi_proc_hamming_distance(samples): # specifically created function to use with pool
return hamming_distance(calls[samples[0]],calls[samples[1]]), samples[0], samples[1]
当我在代码中调用它们时,出现此错误:
NameError: name 'calls' is not defined
我的印象是,嵌套函数可以访问该函数之外的变量。有人可以向我解释为什么我会收到此错误吗?
我意识到解决方案之一就是将字典作为参数传递给第二个函数,这就是我解决问题的方法,但是却增加了运行时间。此外,当我在jupyter上运行代码而不包装print_hamming_distance(calls)时,它就起作用了。
没有包装,我的意思是这样:
def multi_proc_hamming_distance(samples): # specifically created function to use with pool
return hamming_distance(calls[samples[0]],calls[samples[1]]), samples[0], samples[1]
#calls is already defined somewhere
samples = calls.keys()
with Pool(8) as pool: #Parallel Process
for dist, sample1, sample2 in pool.imap(multi_proc_hamming_distance, itertools.combinations(samples,2)):
print( dist, sample1, sample2 )
编辑:完全回溯错误
multiprocessing.pool.RemoteTraceback:
"""
Traceback (most recent call last):
File "/home/usr/anaconda3/envs/some_env/lib/python3.5/multiprocessing/pool.py", line 119, in worker
result = (True, func(*args, **kwds))
File "/usr/project/pipeline/project_name/distance.py", line 44, in multi_proc_hamming_distance
return hamming_distance(calls[samples[0]],calls[samples[1]]), samples[0], samples[1]
NameError: name 'calls' is not defined
"""
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/project/pipeline/project_name.py", line 264, in <module>
main()
File "/usr/project/pipeline/project_name.py", line 259, in main
distance(param)
File "/usr/project/pipeline/project_name.py", line 169, in distance
distance = get_distance[param.data_type](calls)
File "/usr/project/pipeline/project_name/distance.py", line 37, in get_param_type_distance
for dist, sample1, sample2 in pool.imap(multi_proc_hamming_distance, itertools.combinations(samples,2)):
File "/home/usr/anaconda3/envs/some_env/lib/python3.5/multiprocessing/pool.py", line 731, in next
raise value
答案 0 :(得分:0)
是的,嵌套函数可以访问该函数之外的变量。但是在您的情况下,calls变量未在函数内部定义,它只是一个参数,不能由嵌套函数访问。您可以通过如下添加calls = calls
来纠正该错误。
def print_hamming_distance(calls):
#calls is a dictionary
calls = calls
samples = calls.keys()
with Pool(8) as pool: #Parallel Process
for dist, sample1, sample2 in pool.imap(multi_proc_hamming_distance, itertools.combinations(samples,2)):
print( dist, sample1, sample2 )
# nested function
def multi_proc_hamming_distance(calls,samples): # specifically created function to use with pool
return hamming_distance(calls[samples[0]],calls[samples[1]]), samples[0], samples[1]