Python多线程中join函数与setDaemon函数使用说明

      在Python多线程编程的时候,经常需要用到join函数和setDaemon函数。之前对这两个函数一直理解不是很到位。今天查阅了很多资料,对两个函数的认识更加的深入一些了。

      join([timeout])可以参考Python文档说明。大概意思就是调用join函数会使得主调线程阻塞,直到被调用线程运行结束或超时。参数timeout是一个数值类型,用来表示超时时间,如果未提供该参数,那么主调线程将一直阻塞直到被调用线程结束。

      借鉴一下别人的代码说明情况:

 
 
#!/usr/bin/env python
import threading
import time
 
 
 
 
class MyThread(threading.Thread):
 
 
    def __init__(self, func, args, name=''):
        threading.Thread.__init__(self)
        self.name=name
        self.func=func
        self.args=args
 
 
    def run(self):
        apply(self.func, self.args)
 
 
 
 
def print_func(num):
 
 
    while True:
        print "I am thread%d" % num
        time.sleep(1)
 
 
threads = []
t1 = MyThread(print_func, (1, ), print_func.__name__)
threads.append(t1)
t2 = MyThread(print_func, (2, ), print_func.__name__)
threads.append(t2)
 
 
for t in threads:
    t.start()
    t.join()
 
 
print "ok\n"
 
 

      程序的运行输出如下:

2

      查看程序输出发现只有第一个子线程在调用,第二个子线程以及父线程都没有继续走下去。这是因为join函数一直在等待子线程结束,但是循环使得子线程一直没有结束,这样后续子线程和主线程都阻塞了。使得只有第一个子线程在循环执行。

      改一下最后面的代码为:

 
 
for t in threads:
    t.start()
for t in threads:
    t.join()
 
 

      运行结果如下所示:

3

      可以看到线程一和线程二在交替执行。两个子线程完成之前,父线程的print "ok\n"都不会执行。

      修改一下使得两个子线程运行时间不一样,会是怎样的结果呢?

 
 
#!/usr/bin/env python
import threading
import time
 
 
 
 
class MyThread(threading.Thread):
 
 
    def __init__(self, func, args, name=''):
        threading.Thread.__init__(self)
        self.name=name
        self.func=func
        self.args=args
 
 
    def run(self):
        apply(self.func, self.args)
 
 
 
 
def print_func1(num):
 
 
    while True:
        print "I am thread%d" % num
        time.sleep(1)
 
 
def print_func2(num):
 
 
    while True:
        print "I am thread%d" % num
        time.sleep(2)
 
 
threads = []
t1 = MyThread(print_func1, (1, ), print_func1.__name__)
threads.append(t1)
t2 = MyThread(print_func2, (2, ), print_func2.__name__)
threads.append(t2)
 
 
for t in threads:
    t.start()
for t in threads:
    t.join()
 
 
print "ok\n"
 
 

      运行结果如下图所示:

4 

      可以看到一个子线程的完成不会影响另外一个子线程,父线程仍然一直被阻塞,需要等待两个子线程完成之后才会打印结果。

      setDaemon()可以参考Python文档说明。大概意思就是可以设置setDaemon的参数为True来表示将该线程指定为守护线程,如果参数为False就不指定线程为守护线程。设置setDaemon的参数为True之后。主线程和子线程会同时运行,主线程结束运行后,无论子线程运行与否,都会和主线程一起结束。

      借鉴一下别人的代码说明情况:

 
 
#!/usr/bin/env python
import threading
import time
 
 
 
 
class MyThread(threading.Thread):
 
 
    def __init__(self, func, args, name=''):
        threading.Thread.__init__(self)
        self.name=name
        self.func=func
        self.args=args
 
 
    def run(self):
        apply(self.func, self.args)
 
 
 
 
def print_func(num):
 
 
    while True:
        print "I am thread%d" % num
        time.sleep(1)
 
 
threads = []
t1 = MyThread(print_func, (1, ), print_func.__name__)
threads.append(t1)
t2 = MyThread(print_func, (2, ), print_func.__name__)
threads.append(t2)
 
 
 
 
 
 
for t in threads:
    t.setDaemon(True)
    t.start()
 
 
print "ok\n"
 
 

      运行结果如下图所示:

1

      查看程序输出后可以发现print_func函数中的循环没有继续执行下去就退出了,由于setDaemon(True)把子线程设置为守护线程,子线程启动后,父线程也继续执行下去,当父线程执行完最后一条语句print "ok\n"后,没有等待子线程是否完成,直接就退出了,同时子线程也一同结束。

转载于:https://www.cnblogs.com/hiccup/p/5423964.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值