Python 多线程 start()和run()方法的区别(转)

本文通过实例代码对比分析了Python中线程的start()和run()方法启动线程的不同。start()方法会创建新线程并执行,而run()方法只是在主线程中调用目标函数,不会启动新线程。在多线程场景下,start()能实现并发执行,而run()则顺序执行。
摘要由CSDN通过智能技术生成

转自:https://www.cnblogs.com/i-honey/p/8043648.html

在实例调用的函数中加入打印当前线程的名字,分别用start()方法和run()方法启动线程检查有什么区别:

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

import threading

import time

 

def worker():

    count = 1

    while True:

        if count >= 6:

            break

        time.sleep(1)

        count += 1

        print("thread name = {}".format(threading.current_thread().name))#当前线程名

 

= threading.Thread(target=worker,name="MyThread")

t.start()

 

print("===end===")

 

运行结果:

===end===

thread name = MyThread #

thread name = MyThread

thread name = MyThread

thread name = MyThread

thread name = MyThread

  从上面例子中打印的线程名字来看,使用start()方法启动的线程名是我们定义线程对象时设置的name="MyThread"的值,如果没有设置name参数值,则会打印系统分配的Thread-1,Thread-2...这样的名称。

run()方法:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

import threading

import time

 

def worker():

    count = 1

    while True:

        if count >= 6:

            break

        time.sleep(1)

        count += 1

        print("thread name = {}".format(threading.current_thread().name))

 

= threading.Thread(target=worker,name="MyThread")

t.run()

 

print("===end===")

 

运行结果:

thread name = MainThread #

thread name = MainThread

thread name = MainThread

thread name = MainThread

thread name = MainThread

===end===

  上面例子中,使用的是用run()方法启动线程,它打印的线程名是MainThread,也就是主线程。

 

再看下多线程时的例子:

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

import threading

import time

 

def worker():

    count = 1

    while True:

        if count >= 6:

            break

        time.sleep(1)

        count += 1

        print("thread name = {}, thread id = {}".format(threading.current_thread().name,threading.current_thread().ident))

 

t1 = threading.Thread(target=worker,name="t1")

t2 = threading.Thread(target=worker,name='t2')

 

t1.start()

t2.start()

 

print("===end===")

 

运行结果:

===end===

thread name = t1, thread id = 6032

thread name = t2, thread id = 880

thread name = t1, thread id = 6032

thread name = t2, thread id = 880

thread name = t2, thread id = 880

thread name = t1, thread id = 6032

thread name = t1, thread id = 6032

thread name = t2, thread id = 880

thread name = t2, thread id = 880

thread name = t1, thread id = 6032

  上面例子中,start()方法启动了两个新的子线程并交替运行,每个子进程ID也不同。

run():

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

import threading

import time

 

def worker():

    count = 1

    while True:

        if count >= 6:

            break

        time.sleep(1)

        count += 1

        print("thread name = {}, thread id = {}".format(threading.current_thread().name,threading.current_thread().ident))

 

t1 = threading.Thread(target=worker,name="t1")

t2 = threading.Thread(target=worker,name='t2')

 

t1.run()

t2.run()

 

print("===end===")

 

运行结果:

thread name = MainThread, thread id = 2000

thread name = MainThread, thread id = 2000

thread name = MainThread, thread id = 2000

thread name = MainThread, thread id = 2000

thread name = MainThread, thread id = 2000

thread name = MainThread, thread id = 2000

thread name = MainThread, thread id = 2000

thread name = MainThread, thread id = 2000

thread name = MainThread, thread id = 2000

thread name = MainThread, thread id = 2000

===end===

  上面例子中,两个子线程都用run()方法启动,但却是先运行t1.run(),运行完之后才按顺序运行t2.run(),两个线程都工作在主线程,没有启动新线程,因此,run()方法仅仅是普通函数调用。

 

一个进程中至少有一个线程,并作为程序的入口,这个线程就是主线程。
一个进程至少有一个主线程,其它线程称为工作线程。

 

总结:

好了,从上面四个小例子,我们可以总结出:

  • start() 方法是启动一个子线程,线程名就是我们定义的name
  • run() 方法并不启动一个新线程,就是在主线程中调用了一个普通函数而已。

 

因此,如果你想启动多线程,就必须使用start()方法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值