Python3 -- 多线程(threading模块、queue模块)
2021-05-11 14:29
                         标签:阻塞   thread   异常   als   创建   pre   col   内容   python3      Python3 -- 多线程(threading模块、queue模块) 标签:阻塞   thread   异常   als   创建   pre   col   内容   python3    原文地址:https://www.cnblogs.com/gengyufei/p/13151597.html队列模块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()
文章标题:Python3 -- 多线程(threading模块、queue模块)
文章链接:http://soscw.com/essay/84305.html