美文网首页
【Python Threading 学习笔记】2、添加线程

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

作者: TeamsSix | 来源:发表于2019-11-01 12:04 被阅读0次

往期内容:
1、什么是多线程?

这一节主要学习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)>

更多信息欢迎关注微信公众号:TeamsSix
原文地址:https://www.teamssix.com/year/191101-112015.html
参考文章:https://morvanzhou.github.io/tutorials/python-basic/threading
代码项目地址:https://github.com/teamssix/Python-Threading-study-notes

相关文章

网友评论

      本文标题:【Python Threading 学习笔记】2、添加线程

      本文链接:https://www.haomeiwen.com/subject/cmefbctx.html