multiprocessing pool not working in nested functions

前端 未结 1 806
被撕碎了的回忆
被撕碎了的回忆 2020-12-20 03:23

Following code not executing as expected.

import multiprocessing

lock = multiprocessing.Lock()
def dummy():
    def log_results_l1(results):
        lock.ac         


        
相关标签:
1条回答
  • 2020-12-20 04:11

    multiprocessing.Pool methods like the apply* and *map* methods have to pickle both the function and the arguments. Functions are pickled by their qualified name; essentially, on unpickling, the other process needs to be able to import the module they were defined in and do a getattr call to find the function in question. Nested functions aren't available by name outside the function they were defined in, so pickling fails. When you move the function to global scope, you fix this, which is why it works when you do that.

    0 讨论(0)
提交回复
热议问题