How efficient is threading in Python?

后端 未结 6 2059
故里飘歌
故里飘歌 2020-12-16 00:44

I heard threading is not very efficient in Python (compared to other languages).

Is this true? If so, how can a Python programmer overcome this?

6条回答
  •  一整个雨季
    2020-12-16 01:17

    after finding solution (below) I wonder why python still has threading class

    from multiprocessing import Pool
    
    def some_function(x):
        return x*x
    
    Xs = [i for i in xrange(1, 1000)]      # make 1, 2, ..., 999, 1000
    
    pool = Pool(processes=16)              # start 16 worker processes on 16 CPU
    print pool.map(some_function, Xs)      # print out 
    

提交回复
热议问题