从您的帖子中看不出
generate_features
然而,如果有
result1
,
result2
或
filename
是不可序列化的,那么由于某种原因,多处理库将不会调用回调函数,也不会以静默方式调用。我
认为
这是因为多处理库试图在子进程和父进程之间来回传递对象之前对对象进行pickle。如果返回的内容不是“可pickle”(即不可序列化),则不会调用回调。
我自己也遇到过这个bug,它原来是一个给我带来麻烦的logger对象的实例。下面是一些复制我的问题的示例代码:
import multiprocessing as mp
import logging
def bad_test_func(ii):
print('Calling bad function with arg %i'%ii)
name = "file_%i.log"%ii
logging.basicConfig(filename=name,level=logging.DEBUG)
if ii < 4:
log = logging.getLogger()
else:
log = "Test log %i"%ii
return log
def good_test_func(ii):
print('Calling good function with arg %i'%ii)
instance = ('hello', 'world', ii)
return instance
def pool_test(func):
def callback(item):
print('This is the callback')
print('I have been given the following item: ')
print(item)
num_processes = 3
pool = mp.Pool(processes = num_processes)
results = []
for i in range(5):
res = pool.apply_async(func, (i,), callback=callback)
results.append(res)
pool.close()
pool.join()
def main():
print('#'*30)
print('Calling pool test with bad function')
print('#'*30)
pool_test(bad_test_func)
print('#'*30)
print('Calling pool test with good function')
print('#'*30)
pool_test(good_test_func)
if __name__ == '__main__':
main()
希望这有帮助,并为您指明正确的方向。