python线程如何创建和传参
admin
2023-01-29 07:00:04
0

一.线程解释

线程是cpu最小调度单位,一个程序中至少有一个或者多个线程(至于进程暂时不做讲解,后面文章会有详细解释)!在开发中使用线程可以让程序运行效率更高,多线程类似于同时执行多个不同代码块。

二.线程创建和启动

1.导入线程模块

1

2

# 导入线程threading模块

import threading

2.创建线程并初始化线程

调用threading模块中的缺省函数Thread,创建并初始化线程,返回线程句柄。如果对缺省函数已经忘记的小伙伴请回到 python函数的声明和定义中关于缺省参数部分复习一下。

1

2

# 创建并初始化线程,返回线程句柄

t = threading.Thread(target=函数名)

3.启动线程

通过初始化返回的线程句柄调用start()函数,启动线程,此时会自动执行在创建线程时target对应的函数内部的代码:

1

2

# 启动线程

t.start()

综合上面三点,下面使用代码对python线程thread做详细讲解:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

# !usr/bin/env python

# -*- coding:utf-8 _*-

"""

@Author:何以解忧

@Blog(个人博客地址): shuopython.com

@WeChat Official Account(微信公众号):猿说python

@Github:www.github.com

@File:python_thread.py

@Time:2019/10/16 21:02

 

@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!

"""

# 导入线程threading模块

import threading

# 导入内置模块time

import time

 

def wash_clothes():

    print("洗衣服开始...")

    # sleep 5 秒,默认以秒为单位

    time.sleep(5)

    print("洗衣服完成...")

 

def clean_room():

    print("打扫房间开始...")

    # sleep 5 秒,默认以秒为单位

    time.sleep(5)

    print("打扫房间完成...")

 

if __name__ == "__main__":

 

    # 创建线程并初始化 -- 该线程执行wash_clothes中的代码

    t1 = threading.Thread(target=wash_clothes)

     # 创建线程并初始化 -- 该线程执行clean_room中的代码

    t2 = threading.Thread(target=clean_room)

 

    t1.start()

    t2.start()

输出结果:

1

2

3

4

洗衣服开始...

打扫房间开始...

洗衣服完成...

打扫房间完成...

运行程序可以发现程序从运行开始到结束,一共耗时5秒时间!注意观察输出日志:

  • 一:洗衣服开始和打扫房间开始几乎同时开始,两个事件同时执行.

  • 二:程序停止5秒;

  • 三:洗衣服和打扫房间几乎同时完成

当然你也可以按照以前的学习的内容,先调用wash_clothes函数,在调用clean_room函数,同样能输出内容,而耗时却是10秒左右,示例代码如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

# 导入内置模块time

import time

 

def wash_clothes():

    print("洗衣服开始...")

    # sleep 5 秒,默认以秒为单位

    time.sleep(5)

    print("洗衣服完成...")

 

def clean_room():

    print("打扫房间开始...")

    # sleep 5 秒,默认以秒为单位

    time.sleep(5)

    print("打扫房间完成...")

 

if __name__ == "__main__":

 

    wash_clothes()

    clean_room()

输出结果:

1

2

3

4

洗衣服开始...

洗衣服完成...

打扫房间开始...

打扫房间完成...

运行程序可以发现程序从运行开始到结束,一共耗时10秒时间!注意观察输出日志:

  • 一:洗衣服开始;

  • 二:程序停止了5秒;

  • 三:洗衣服完成,打扫房间开始

  • 四:程序停止5秒;

  • 五:打扫房间结束,程序结束;

由此可见:多线程可以同时运行多个任务,效率远比单线程更高!

三.线程传参

在上面的demo中,我们并没有为线程传递参数,如果在线程中需要传递参数怎么办呢?

threading.Thread()函数中有两个缺省参数 args 和 kwargs ,args 是元组类型,kwargs 是字典类型,缺省值默认为空,除此之外,其实还可以设置线程的名字等,其函数声明如下:

(ps:如果对缺省函数已经忘记的小伙伴请回到 python函数的声明和定义中关于缺省参数部分复习一下)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

def __init__(self, group=None, target=None, name=None,

             args=(), kwargs=None, *, daemon=None):

    """This constructor should always be called with keyword arguments. Arguments are:

 

    *group* should be None; reserved for future extension when a ThreadGroup

    class is implemented.

 

    *target* is the callable object to be invoked by the run()

    method. Defaults to None, meaning nothing is called.

 

    *name* is the thread name. By default, a unique name is constructed of

    the form "Thread-N" where N is a small decimal number.

 

    *args* is the argument tuple for the target invocation. Defaults to ().

 

    *kwargs* is a dictionary of keyword arguments for the target

    invocation. Defaults to {}.

 

    If a subclass overrides the constructor, it must make sure to invoke

    the base class constructor (Thread.__init__()) before doing anything

    else to the thread.

 

    """

示例代码如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

# 导入线程threading模块

import threading

# 导入内置模块time

import time

 

def wash_clothes(*args,**kargcs):

    print("wash_clothes:",args)

    print("wash_clothes:", kargcs)

 

def clean_room(*args,**kargcs):

    print("clean_room:",args)

    print("clean_room:", kargcs)

 

if __name__ == "__main__":

 

    t1 = threading.Thread(target=wash_clothes,

                          args=(1,"猿说python"),   # args 传递元组,可以同时传递多个数据

                          kwargs={"a":1,"b":False}) # kwargs 传递字典,可以同时传递多个键值对

 

    t2 = threading.Thread(target=clean_room,

                          args=(2,False), # args 传递元组,可以同时传递多个数据

                          kwargs={"c":0.2,"d":False}) # kwargs 传递字典,可以同时传递多个键值对

 

    t1.start()

    t2.start()

四.线程结束

值得思考的是:在上面这份代码中一共有几个线程呢?并非两个,一共是三个线程:

  • 线程一:__name__ == “__main__” 作为主线程;

  • 线程二:t1 作为子线程;

  • 线程三:t2 作为子线程;

注意:主程序会等待所有子程序结束之后才会结束!

五.相关函数介绍

1.threading.Thread() — 创建线程并初始化线程,可以为线程传递参数 ;

2.threading.enumerate() — 返回一个包含正在运行的线程的list;

3.threading.activeCount(): 返回正在运行的线程数量,与len(threading.enumerate())有相同的结果;

4.Thread.start() — 启动线程 ;

5.Thread.join() — 阻塞函数,一直等到线程结束为止 ;

6.Thread.isAlive() — 返回线程是否活动的;

7.Thread.getName() — 返回线程名;

8.Thread.setName() — 设置线程名;

9.Thread.setDaemon() — 设置为后台线程,这里默认是False,设置为True之后则主线程不会再等待子线程结束才结束,而是主线程结束意味程序退出,子线程也立即结束,注意调用时必须设置在start()之前;

简单的示例代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

# 导入线程threading模块

import threading

# 导入内置模块time

import time

 

def wash_clothes(*args,**kargcs):

    time.sleep(2)

    print("wash_clothes:",args)

    time.sleep(2)

    print("wash_clothes:", kargcs)

 

def clean_room(*args,**kargcs):

    time.sleep(2)

    print("clean_room:",args)

    time.sleep(2)

    print("clean_room:", kargcs)

 

if __name__ == "__main__":

 

    t1 = threading.Thread(target=wash_clothes,

                          args=(1,"猿说python"),   # args 传递元组,可以同时传递多个数据

                          kwargs={"a":1,"b":False}) # kwargs 传递字典,可以同时传递多个键值对

 

    t2 = threading.Thread(target=clean_room,

                          args=(2,False), # args 传递元组,可以同时传递多个数据

                          kwargs={"c":0.2,"d":False}) # kwargs 传递字典,可以同时传递多个键值对

 

 

    # setDaemon(True)意味着主线程退出,不管子线程执行到哪一行,子线程自动结束

    # t1.setDaemon(True)

    # t2.setDaemon(True)

    t1.start()

    t2.start()

 

    print("threading.enumerate():",threading.enumerate())

    print("threading.activeCount():", threading.activeCount())

    print("t1.isAlive():",t1.isAlive())

    print("t1.getName():", t1.getName())

    print("t2.isAlive():", t2.isAlive())

    t2.setName("my_custom_thread_2")

    print("t2.getName():", t2.getName())

输出结果:

1

2

3

4

5

6

7

8

9

10

threading.enumerate(): [<_MainThread(MainThread, started 18388)>, <Thread(Thread-1, started 16740)>, <Thread(Thread-2, started 17888)>]

threading.activeCount(): 3

t1.isAlive(): True

t1.getName(): Thread-1

t2.isAlive(): True

t2.getName(): my_custom_thread_2

clean_room: (2, False)

wash_clothes: (1, '猿说python')

wash_clothes: {'a': 1, 'b': False}

clean_room: {'c': 0.2, 'd': False}

相关内容

热门资讯

德国总理:美国正在被伊朗羞辱 德国之声4月27日报道,德国总理默茨在访问一所学校时表示,在当前的持续冲突中,伊朗领导层正试图羞辱美...
理响中国|“长”歌以行,风云激... 光阴如梭,东方潮阔。这里是中国的长三角,世界的长三角。无论过去、现在还是未来,这片土地都因时代而生,...
白宫:特朗普及其国安团队开会讨... 新华社华盛顿4月27日电 美国白宫新闻秘书莱维特27日在记者会上证实,总统特朗普及其国家安全团队当天...
人民日报刊文:日本放开杀伤性武... 日本放开杀伤性武器出口推高地缘冲突风险(国际论坛)常思纯《人民日报》(2026年04月28日 第 0...
医疗保障法草案二审:明确生育保... 满足多样化健康保障需求本报记者 彭 波4月27日,医疗保障法草案二审稿提请十四届全国人大常委会第二十...
天津一景区发生自转旋翼机事故1... 澎湃新闻记者 吕新文中国民用航空华北地区管理局4月22日公布《豪客通航“10•1”天津长芦汉盐旅游区...
卡塔尔埃米尔与美国总统特朗普通... 当地时间24日,卡塔尔埃米尔塔米姆与美国总统特朗普通电话,重点就中东地区局势以及伊朗与美国谈判问题交...
男子30年前被扣押2859克黄... 澎湃新闻记者 王鑫家住辽宁省大连市的潘永嘉近日向澎湃新闻反映称,三十年前,他在大连周水子机场被盖州市...
商务部:取消反制欧盟两家金融机... 中华人民共和国商务部令二〇二六年 第1号鉴于欧盟已取消对中国两家金融机构的制裁措施,现公布《关于取消...
过去24小时共有5艘船只通过霍... 总台记者当地时间24日获悉,过去24小时内,共有5艘船只通过霍尔木兹海峡,其中包括一艘伊朗油轮。(总...