我有一個函數slow_function
需要大約200秒來處理一個job_title
,它讀寫一個全局變量。與不同參數並行運行一個函數 - python
使用此代碼的性能沒有提高。我是否錯過了一些東西,但是這會返回相同的結果。
代碼並行運行五個的工作類別:
from threading import Thread
threads = []
start = time.time()
for job_title in self.job_titles:
t = Thread(target=self.slow_function, args=(job_title,))
threads.append(t)
# Start all threads
for x in threads:
x.start()
# Wait for all of them to finish
for x in threads:
x.join()
end = time.time()
print "New time taken for all jobs:", end - start
請閱讀這一個http://stackoverflow.com/questions/32657840/is-it-possible-to-avoid-locking-overhead-when-sharing-類型的字典之間線程-IN-p/32659672#32659672。我不確定你的問題是否重複,但它可以幫助你。 – Jimilian
謝謝。你的解決方案爲我工作。 – Azhar