美文网首页
Python多线程(二):多线程编程

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

作者: NWKYEKJ | 来源:发表于2019-03-15 10:18 被阅读0次

上一篇:GIL

多线程编程有两种方式,一种是通过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方法。

相关文章

  • Python多线程编程——多线程编程中的加锁机制

    如果大家对Python中的多线程编程不是很了解,推荐大家阅读之前的两篇文章:Python多线程编程——多线程基础介...

  • Python 并行编程

    多线程编程 Python 主要提供了包括thread、threading、Queue等多线程编程模块。thread...

  • 多线程编程

    多线程编程之Linux环境下的多线程(一)多线程编程之Linux环境下的多线程(二)多线程编程之Linux环境下的...

  • python多线程入门之旅一

    所有代码来自python核心编程 参考python核心编程一书,学习多线程工作模式,多线程实现主要模块thread...

  • 5-线程(补充)

    Python多线程原理与实战 目的: (1)了解python线程执行原理 (2)掌握多线程编程与线程同步 (3)了...

  • 多线程

    Python多线程原理与实战 目的: (1)了解python线程执行原理 (2)掌握多线程编程与线程同步 (3)了...

  • Python多线程变量优化—threadLocal

    Python多线程变量优化—threadLocal 再多线程的环境下,每个线程都有自己的数据。在多线程编程中应该尽...

  • Python 3 多线程编程

    本文主要基于python 3.5实现多线程编程 1. 创建多线程 2. 多线程间的同步,lock机制 3. que...

  • python3 多线程编程实战: http多线程下载器的编写

    python3 多线程编程实战: http多线程下载器的编写说到多线程的应用,这种并发下载的情况显然比较适合。也是...

  • Python 并发编程简介

    1 多线程和多进程 Python语言中既有多线程编程也有多进程编程,也叫做并发编程。 多进程 把一个程序分成几个不...

网友评论

      本文标题:Python多线程(二):多线程编程

      本文链接:https://www.haomeiwen.com/subject/jmzupqtx.html