Python中主进程(process)与子线程(Thread)关系实例

本文通过实例解析Python中的主进程与子线程关系,重点介绍join函数和setDaemon函数的使用。join函数使得主进程等待子线程结束后再继续执行,而setDaemon则使主进程不等待子线程,直接结束。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

https://blog.youkuaiyun.com/oatnehc/article/details/50616835

https://www.cnblogs.com/ArsenalfanInECNU/p/8989770.html

之前用python的多线程,总是处理不好进程和线程之间的关系。后来发现了join和setDaemon函数,才终于弄明白。下面总结一下。

1.使用join函数后,主进程会在调用join的地方等待子线程结束,然后才接着往下执行。

join使用实例如下:

 

 
  1. import time

  2. import random

  3. import threading

  4.  
  5. class worker(threading.Thread):

  6. def __init__(self):

  7. threading.Thread.__init__(self)

  8. def run(self):

  9. t = random.randint(1,10)

  10. time.sleep(t)

  11. print "This is " + self.getName() + ";I sleep %d second."%(t)

  12.  
  13. tsk = []

  14. for i in xrange(0,5):

  15. time.sleep(0.1)

  16. thread = worker()

  17. thread.start()

  18. tsk.append(thread)

  19. for tt in tsk:

  20. tt.join()

  21. print "This is the end of main thread."

运行结果如下:

 

 

 
  1. # python testjoin.py

  2. This is Thread-3;I sleep 2 second.

  3. This is Thread-1;I sleep 4 second.

  4. This is Thread-2;I sleep 7 second.

  5. This is Thread-4;I sleep 7 second.

  6. This is Thread-5;I sleep 7 second.

  7. This is the end of main thread.

 

 

这里创建了5个子线程,每个线程随机等待1-10秒后打印退出;主线程分别等待5个子线程结束。最后结果是先显示各个子线程,再显示主进程的结果。

2. 如果使用的setDaemon函数,则与join相反,主进程结束的时候不会等待子线程。

setDaemon函数使用实例:

 
  1. import time

  2. import random

  3. import threading

  4.  
  5. class worker(threading.Thread):

  6. def __init__(self):

  7. threading.Thread.__init__(self)

  8. def run(self):

  9. t = random.randint(1,10)

  10. time.sleep(t)

  11. print "This is " + self.getName() + ";I sleep %d second."%(t)

  12.  
  13. tsk = []

  14. for i in xrange(0,5):

  15. time.sleep(0.1)

  16. thread = worker()

  17. thread.setDaemon(True)

  18. thread.start()

  19. tsk.append(thread)

  20. print "This is the end of main thread."

这里设置主进程为守护进程,当主进程结束的时候,子线程被中止

 

运行结果如下:

 

 
  1. #python testsetDaemon.py

  2. This is the end of main thread.

 

3、如果没有使用join和setDaemon函数,则主进程在创建子线程后,直接运行后面的代码,主程序一直挂起,直到子线程结束才能结束。

 

 
  1. import time

  2. import random

  3. import threading

  4.  
  5. class worker(threading.Thread):

  6. def __init__(self):

  7. threading.Thread.__init__(self)

  8. def run(self):

  9. t = random.randint(1,10)

  10. time.sleep(t)

  11. print "This is " + self.getName() + ";I sleep %d second."%(t)

  12.  
  13. tsk = []

  14. for i in xrange(0,5):

  15. time.sleep(0.1)

  16. thread = worker()

  17. thread.start()

  18. tsk.append(thread)

  19. print "This is the end of main thread."

运行结果如下:

 

 

 
  1. # python testthread.py

  2. This is the end of main thread.

  3. This is Thread-4;I sleep 1 second.

  4. This is Thread-3;I sleep 7 second.

  5. This is Thread-5;I sleep 7 second.

  6. This is Thread-1;I sleep 10 second.

  7. This is Thread-2;I sleep 10 second.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值