首页 > 编程语言 > 详细

Python3 -- 多线程(threading模块、queue模块)

时间:2020-06-17 12:37:05      阅读:65      评论:0      收藏:0      [点我收藏+]

队列模块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模块)

原文:https://www.cnblogs.com/gengyufei/p/13151597.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!