Python多线程(二):多线程编程

转载自:https://www.jianshu.com/p/c1d001564a0c

多线程编程有两种方式,一种是通过Thread类对线程进行实例化,另外一种是通过继承Thread类并重写其run方法。

通过Thread类实例化进行多线程编程
下面是一个例子:

import threading
import time

def do_something(content, sec):
    print('%s started' % content)
    time.sleep(sec)
    print('%s completed' % content)

def main():
    thread1 = threading.Thread(target=do_something, args=('First task', 2))
    thread2 = threading.Thread(target=do_something, args=('Second task', 4))
    start_time = time.time()
    thread1.start()
    thread2.start()
    print('Last time: %fs' % (time.time() - start_time))

main()

从这个例子中我们可以很容易地看出如果用Thread类实例化的方式创建线程,并通过start()方法开始线程。
结果输入如下:

First task started
Second task started
Last time: 0.000294s
First task completed
Second task completed

为什么这里的时间会是0s呢?原因是因为当我们创建了两个线程并启动后,此时的程序共有三个线程,thread1thread2为子线程,main函数的线程被称为主线程,可在线程内部通过threading.current_thread()来获取当前线程信息,主线程会默认明名为'MainThread',可在创建线程时使用参数name标记线程名。当开始了两个子线程后,由于三个线程并行,主线程也要继续运行,而执行两个start()方法的时间很短,所以打印的时间是0s。并且这里的输出结果包含了线程结束语句'... completed',说明主线程运行结束,程序并没有退出,而是要等子线程运行结束后再退出。

如何使得主线程退出后子线程自动退出呢?只需要对子线程调用setDaemon方法,将子线程标记成守护线程,如下所示:

def main():
    thread1 = threading.Thread(target=do_something, args=('First task', 2))
    thread2 = threading.Thread(target=do_something, args=('Second task', 4))
    thread1.setDaemon(True)
    thread2.setDaemon(True)
    start_time = time.time()
    thread1.start()
    thread2.start()
    print('Last time: %fs' % (time.time() - start_time))

main()

我们再运行程序,得到以下结果:

First task started
Second task started
Last time: 0.000235s

和我们预想的一致,主线程退出后,子线程也退出了,没来得及打印'... completed'语句。

另外一个问题是如何将主线程阻塞,等子线程运行完成后再继续主线程,这样我们就可以获得两个线程并行运行的时间了。只需要对子线程调用join方法就可以将主线程阻塞,如下所示:

def main():
    thread1 = threading.Thread(target=do_something, args=('First task', 2))
    thread2 = threading.Thread(target=do_something, args=('Second task', 4))
    start_time = time.time()
    thread1.start()
    thread2.start()
    thread1.join()
    thread2.join()
    print('Last time: %fs' % (time.time() - start_time))

main()

运行结果如下:

First task started
Second task started
First task completed
Second task completed
Last time: 4.004622s

从结果可以看出,运行时间并不是串行的6s,而是两者中的大者,原因在前一章中提到了,当线程遇到 IO 操作或者time.sleep时,GIL 会释放,将执行权留给其他线程。

通过继承 Thread 类并重写其run方法进行多线程编程
上面的方法适用于逻辑简单明确的情况,当代码逻辑复杂时,最好使用这种方法,方便代码维护。

如果你明白了上面的方法,只需要进行细微的改动即可,下面是一个例子:

import threading

class DoSomething(threading.Thread):
    def __init__(self, content, sec):
        super().__init__()
        self.content = content
        self.sec = sec
    def run(self):
        print('%s started' % self.content)
        time.sleep(self.sec)
        print('%s completed' % self.content)

def main():
    thread1 = DoSomething('First task', 2)
    thread2 = DoSomething('Second task', 4)
    thread1.start()
    thread2.start()
    thread1.join()
    thread2.join()

main()

可以看出,threading.Thread类的start方法,实际上就是运行其run方法。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值