qq_花開(kāi)花謝_0
2022-10-06 16:09:49
所以我試圖比較threading是更快還是multiprocessing。理論上,由于GIL,多處理應(yīng)該比多線程更快,因?yàn)橐淮沃贿\(yùn)行一個(gè)線程。但是我得到了相反的結(jié)果,即線程比多處理花費(fèi)的時(shí)間更少,我缺少什么請(qǐng)幫忙。下面是線程的代碼import threadingfrom queue import Queueimport timeprint_lock = threading.Lock()def exampleJob(worker): time.sleep(10) with print_lock: print(threading.current_thread().name,worker)def threader(): while True: worker = q.get() exampleJob(worker) q.task_done()q = Queue()for x in range(4): t = threading.Thread(target=threader) print(x) t.daemon = True t.start()start = time.time()for worker in range(8): q.put(worker)q.join()print('Entire job took:',time.time() - start)下面是多處理的代碼import multiprocessing as mpimport timedef exampleJob(print_lock,worker): # function simulating some computation time.sleep(10) with print_lock: print(mp.current_process().name,worker)def processor(print_lock,q): # function where process pick up the job while True: worker = q.get() if worker is None: # flag to exit the process break exampleJob(print_lock,worker)if __name__ == '__main__': print_lock = mp.Lock() q = mp.Queue() processes = [mp.Process(target=processor,args=(print_lock,q)) for _ in range(4)] for process in processes: process.start() start = time.time() for worker in range(8): q.put(worker) for process in processes: q.put(None) # quit indicator for process in processes: process.join() print('Entire job took:',time.time() - start)
2 回答

呼如林
TA貢獻(xiàn)1798條經(jīng)驗(yàn) 獲得超3個(gè)贊
由于 GIL 的存在,僅當(dāng)您執(zhí)行計(jì)算密集型任務(wù)時(shí),添加到 @zmbq 線程才會(huì)更慢。如果您的操作是 I/O 綁定的并且很少有其他類似的操作,那么線程肯定會(huì)更快,因?yàn)樯婕暗拈_(kāi)銷更少。請(qǐng)參閱以下博客以更好地理解相同的內(nèi)容。
作為數(shù)據(jù)科學(xué)家利用 Python 中的多處理和多線程
希望這可以幫助!

偶然的你
TA貢獻(xiàn)1841條經(jīng)驗(yàn) 獲得超3個(gè)贊
這不是一個(gè)適當(dāng)?shù)臏y(cè)試。time.sleep
可能不會(huì)獲得 GIL,因此您正在運(yùn)行并發(fā)線程與并發(fā)進(jìn)程。線程更快,因?yàn)闆](méi)有啟動(dòng)成本。
您應(yīng)該在線程中執(zhí)行一些計(jì)算,然后您會(huì)看到差異。
添加回答
舉報(bào)
0/150
提交
取消