Python multiprocessing example not working

前端 未结 5 1158
无人及你
无人及你 2020-12-03 02:02

I am trying to learn how to use multiprocessingbut I can\'t get it to work. Here is the code right out of the documentation

from multiprocessin         


        
5条回答
  •  借酒劲吻你
    2020-12-03 02:43

    Most likely your main process exits before sysout is flushed. Try this:

    from multiprocessing import Process
    import sys
    
    def f(name):
        print 'hello', name
    
    if __name__ == '__main__':
        p = Process(target=f, args=('bob',))
        p.start()
        p.join()
        # make sure all output has been processed before we exit
        sys.stdout.flush()
    

    If this doesn't work, try adding time.sleep(1) as the last statement.

提交回复
热议问题