码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2020-06-17 12:28:33      阅读:74      评论:0      收藏:0      [点我收藏+]

标签:阻塞   thread   异常   als   创建   pre   col   内容   python3   

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

标签:阻塞   thread   异常   als   创建   pre   col   内容   python3   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!