队列模块queue:
from queue import Queue
# 使用
q = Queue()
q.put(url) # url ,这里只是举个栗子
# 获取队列内容
q.get() # 当队列为空时,发生阻塞
# 获取队列内容
q.get(block=True, timeout=3) # 超过3秒,抛异常
# 获取队列内容
q.get(block=False) # 队列为空时,直接抛异常
# 判断队列是否为空
q.empty() # 如果队列为空,返回True,反之False
线程模块threading:
from threading import Thread
# 使用流程
t = Thread(target=函数名) # 创建线程对象
t.start() # 创建并启动线程
t.join() # 阻塞等待回收线程
创建多线程:
from threading import Thread
# 使用流程
t_list = []
for i in range(10):
t = Thread(target=函数名) # 创建线程对象
t_list.appent(t)
t.start()
for t in t_list:
t.join()