首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python Threading 学习笔记 | 2、添加线程

Python Threading 学习笔记 | 2、添加线程

作者头像
TeamsSix
发布2019-11-05 01:29:20
4280
发布2019-11-05 01:29:20
举报

这一节主要学习Threading模块的一些基本操作,如获取线程数,添加线程等。

首先导入Threading模块

import threading

获取已激活的线程数

threading.active_count()

查看所有线程信息

threading.enumerate()

查看现在正在运行的线程

threading.current_thread()

添加线程,threading.Thread()接收参数target代表这个线程要完成的任务,需自行定义

import threading
def thread_jobs():  # 定义要添加的线程
print('已激活的线程数:%s' % threading.active_count())
print('所有线程信息:%s' % threading.enumerate())
print('正在运行的线程:%s' % threading.current_thread())
def main():
thread = threading.Thread(target=thread_jobs, )  # 定义线程
thread.start()  # 开始线程
if __name__ == '__main__':
main()

运行结果:

# python 2_add_thread.py
已激活的线程数:2
所有线程信息:[<_MainThread(MainThread, stopped 16800)>, <Thread(Thread-1, started 20512)>]
正在运行的线程 <Thread(Thread-1, started 20512)>

参考文章:https://morvanzhou.github.io/tutorials/python-basic/threading 代码项目地址:https://github.com/teamssix/Python-Threading-study-note往期推荐

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-11-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 TeamsSix 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档