多线程
threading模块
threading.Thread()创建一个线程
start()方法开始线程
join(‘time‘)方法使该线程运行time时间后再进行主线程,默认为结束时守护线程
setDaemon()方法设置该线程为守护线程
主线程结束时子线程也结束线程锁(多个线程对同一个数据进行修改时需要设立线程锁)
lock=threading.lock()
线程开始前lock.acquire()
加锁
线程结束时lock.release()
解锁多线程之间的通信
from queue import Queue
线程池
from concurrent.futures import ThreadPoolExecutor
map()方法加入多个线程
多进程
import multiprocessing
- 类似于多线程使用
- 设置守护进程时用daemon()方法
- 进程池
from concurrent.futures import ProcessPoolExecutor