Python multiprocessing pool, join; not waiting to go on?

前端 未结 1 1303
温柔的废话
温柔的废话 2021-01-01 14:07

(1) I\'m trying to use pool.map followed by pool.join(), but python doesn\'t seem to be waiting for pool.map to finish before going on

1条回答
  •  庸人自扰
    2021-01-01 14:28

    This is not the correct way to use map.

    1. Using a global variable that way is absolutely wrong. Processes do not share the same memory (normally) so every f will have his own copy of foo. To share a variable between different processes you should use a Manager
    2. Function passed to map are, usually, expected to return a value.

    I suggest you to read some documentation.

    However here is a dummy example of how you could implement it:

    from multiprocessing import Pool
    
    foo = {1: []}
    
    def f(x):
        return x
    
    def main():
        pool = Pool()
        foo[1] = pool.map(f, range(100))
        pool.close()
        pool.join()
        print foo
    
    if __name__ == '__main__':
        main()
    

    You may also do something like pool.map(functools.partial(f, foo), range(100)) where foo is a Manager.

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