Python multiprocessing example not working

前端 未结 5 1165
无人及你
无人及你 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:25

    It works.

    I've marked the changes needed to make your sample run using comments:

    from multiprocessing import Process
    
    def f(name):
    print 'hello', name #indent
    
    if __name__ == '__main__':
        p = Process(target=f, args=('bob',))
        p.start()
        p.join()` # remove ` (grave accent)
    

    result:

    from multiprocessing import Process
    
    def f(name):
        print 'hello', name
    
    if __name__ == '__main__':
        p = Process(target=f, args=('bob',))
        p.start()
        p.join()
    

    Output from my laptop after saving it as ex1.py:

    reuts@reuts-K53SD:~/python_examples$ cat ex1.py 
    #!/usr/bin/env python
    from multiprocessing import Process
    
    def f(name):
        print 'hello', name
    
    if __name__ == '__main__':
        p = Process(target=f, args=('bob',))
        p.start()
        p.join()
    reuts@reuts-K53SD:~/python_examples$ python ex1.py 
    hello bob
    

提交回复
热议问题