How can I use threading in Python?

前端 未结 19 2659
迷失自我
迷失自我 2020-11-21 04:54

I am trying to understand threading in Python. I\'ve looked at the documentation and examples, but quite frankly, many examples are overly sophisticated and I\'m having trou

19条回答
  •  不知归路
    2020-11-21 05:22

    Like others mentioned, CPython can use threads only for I/O waits due to GIL.

    If you want to benefit from multiple cores for CPU-bound tasks, use multiprocessing:

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

提交回复
热议问题