Python中有多种方式可以实现并行处理,以下是一些常用的方法:
import threading
def task():
# 任务代码
threads = []
for i in range(10):
t = threading.Thread(target=task)
threads.append(t)
t.start()
for t in threads:
t.join()
from multiprocessing import Process
def task():
# 任务代码
processes = []
for i in range(10):
p = Process(target=task)
processes.append(p)
p.start()
for p in processes:
p.join()
from concurrent.futures import ThreadPoolExecutor
def task():
# 任务代码
with ThreadPoolExecutor() as executor:
results = [executor.submit(task) for _ in range(10)]
for result in results:
result.result()
以上是一些常用的并行处理方法,可以根据具体需求选择合适的方法来实现并行处理。