1.python多線程編程實例
1.1最常用的多線程例子
import threading
import time
def loop(name):
for x in range(10):
print(name + ":" + str(x))
time.sleep(1)
if __name__ == "__main__":
threads_pool = []
for name in ["xxx", "yyy", "zzzz"]:
t = threading.Thread(target=loop, args=(name,))
threads_pool.append(t)
t.start() # 開啟線程任務
for t in threads_pool:
t.join() # 主進程等待子線程完成再結束
print("---done!---")
1.2 線程池threadpool
import threadpool
import time
def show(name):
for x in range(10):
print("name:{}=>x:{}".format(name,x))
time.sleep(1)
if __name__ == "__main__":
# 創建線程池對象
pool = threadpool.ThreadPool(10)
# 組裝任務列表
name_list = ["張三", "李四", "王五"]
requests = threadpool.makeRequests(show, name_list)
# 執行任務
for r in requests:
pool.putRequest(r)
# 主進程阻塞等待
pool.wait()
print("---done!---")
1.3 線程池futures實現
使用map:
無返回值:
import time
from concurrent import futures
def show(name):
for x in range(10):
print("name:{}=>x:{}".format(name,x))
time.sleep(1)
if __name__ == "__main__":
name_list = ["張三", "李四", "王五"]
with futures.ThreadPoolExecutor(max_workers=10) as executor:
executor.map(show, name_list)
print("---done!---")
獲取返回值:
import time
from concurrent import futures
def show(name):
for x in range(10):
print("name:{}=>x:{}".format(name,x))
time.sleep(0.1)
return "我是:" + name
if __name__ == "__main__":
name_list = ["張三", "李四", "王五"]
with futures.ThreadPoolExecutor(max_workers=10) as executor:
future_to_name = executor.map(show, name_list)
print(future_to_name)
for future in future_to_name:
print(future)
print("---done!---")
使用submit:
獲取返回值:
import time
from concurrent import futures
def show(name):
for x in range(10):
print("name:{}=>x:{}".format(name,x))
time.sleep(0.1)
return "我是:" + name
if __name__ == "__main__":
name_list = ["張三", "李四", "王五"]
with futures.ThreadPoolExecutor(max_workers=10) as executor:
future_to_name = [executor.submit(show, name) for name in name_list]
print(future_to_name)
for future in futures.as_completed(future_to_name):
print(future.result())
print("---done!---")
2.python多進程編程實例
2.1 基本例子
from multiprocessing import Process
import time
def show(name):
for x in range(10):
print("name:{}=>x{}".format(name,x))
time.sleep(1)
if __name__ == '__main__':
p1 = Process(target=show, args=("我是你爸爸",))
p2 = Process(target=show, args=("我是你媽媽",))
# 開始多進程任務
p1.start()
p2.start()
# 阻塞等待
p1.join()
p2.join()
print("---done!---")
2.2進程池futures實現
import time
from concurrent import futures
def show(name):
for x in range(10):
print("name:{}=>x:{}".format(name,x))
time.sleep(1)
if __name__ == "__main__":
name_list = ["張三", "李四", "王五"]
with futures.ProcessPoolExecutor(max_workers=10) as executor:
executor.map(show, name_list)
print("---done!---")