Python中如何讓幾條代碼同時執行?
代碼一般是一條一條的執行,必須等上一條執行完了後,才跳到這一行。但是能不能讓幾條代碼同時執行呢?
對於python來說並發有多線程和協程,並行有多進程,看你的需求自己選擇
可以使用pool
和map
的組合來進行簡單的multithreading, 如:
from multiprocessing.dummy import Pool as ThreadPool
pool = ThreadPool(4)
results = pool.map(my_function, my_array)
具體例子:
import urllib2
from multiprocessing.dummy import Pool as ThreadPool
urls = [
http://www.python.org,
http://www.python.org/about/,
http://www.onlamp.com/pub/a/python/2003/04/17/metaclasses.html,
http://www.python.org/doc/,
http://www.python.org/download/,
http://www.python.org/getit/,
http://www.python.org/community/,
https://wiki.python.org/moin/,
]
# Make the Pool of workers
pool = ThreadPool(4)
# Open the URLs in their own threads
# and return the results
results = pool.map(urllib2.urlopen, urls)
# Close the pool and wait for the work to finish
pool.close()
pool.join()
結果
Single thread: 14.4 seconds
4 Pool: 3.1 seconds
8 Pool: 1.4 seconds
13 Pool: 1.3 seconds
Ref: https://stackoverflow.com/questions/2846653/how-can-i-use-threading-in-python
使用一些命令工具進行並行,
簡單點使用一些命令工具進行並行就可以了,比如我介紹的 rush
王詩翔:使用 rush 進行命令並行處理?zhuanlan.zhihu.com推薦閱讀: