知行编程网知行编程网  2022-12-04 00:00 知行编程网 隐藏边栏  1 
文章评分 0 次,平均分 0.0
导语: 本文主要介绍了关于Python中的线程和多线程是什么的相关知识,希望可以帮到处于编程学习途中的小伙伴


什么是Python中的线程和多线程


一、线程的概念

一个进程中至少有一个控制线程。进程的概念只是一个抽象的概念。真正调度到CPU上的是进程中的线程,就像地铁中的线程真正工作在地铁的进程中一样。北京地铁至少要有一根线。线程才是真正的工作。线程使用进程中包含的一堆资源。线程只是一个调度单元,不包含资源。

什么时候需要开启多线程:一个进程中的多个线程共享本进程中的资源,所以如果多个任务共享同一个资源,就需要开启多线程。多线程是指在一个进程中开启多个线程。简单的说:如果多个任务共享同一个资源空间,那么一个进程中肯定要开多个线程。一个进程的这个任务可能对应多个子任务。如果一个进程中只开一个线程,那么多个子任务的执行效果实际上是串行的,即一个程序中只有一条执行路径。

对于计算密集型应用,应该使用多进程;对于IO密集型应用,应该使用多线程。线程的创建比进程的创建开销小的多。


二、Python中线程的特点

1.在其他语言中,一个进程中开启多个线程,每个线程可以被一个CPU使用,但是在python中,一个进程中只能有一个线程同时运行。

2.比如其他语言,比如我现在启动了一个进程,这个进程包含了几个线程。如果我现在有多个cpus,每个线程可以对应对应的CPU。

3.但是在python中,如果我们现在启动一个进程,这个进程对应多个线程,同时只能运行一个线程。对于其他语言,在多CPU系统中,为了最大限度的利用多核,可以开多线程。但是 Python 中的多线程无法发挥多核的优势。

4、同一个进程中,多个线程可以相互通信;但是进程之间的通信必须基于IPC等消息的通信机制(IPC机制包括队列和管道)。在一个进程中,改变主线程可能会影响其他线程的行为,但是改变父进程不会影响其他子进程的行为,因为进程之间是完全隔离的。在python中,同一个进程中只能同时运行一个线程。如果一个线程被系统调用阻塞,整个进程就会被挂起。


三、多线程的理解

多处理和多线程都可以执行多个任务,线程是进程的一部分。线程的特点是线程间可以共享内存和变量,资源消耗低(但是在Unix环境下,多进程和多线程资源调度消耗差距并不明显,Unix调度是快点)。缺点是线程间的同步和加速。锁比较麻烦。


四、Python多线程创建

在Python中,也可以实现多线程。有两个标准模块thread和threading,但我们主要使用更高级的threading模块。使用示例:

import threading
import time
 
def target():
    print 'the curent threading  %s is running' % threading.current_thread().name
    time.sleep(1)
    print 'the curent threading  %s is ended' % threading.current_thread().name
 
print 'the curent threading  %s is running' % threading.current_thread().name
t = threading.Thread(target=target)
 
t.start()
t.join()
print 'the curent threading  %s is ended' % threading.current_thread().name

输出:

the curent threading  MainThread is running
the curent threading  Thread-1 is running
the curent threading  Thread-1 is ended
the curent threading  MainThread is ended

start是启动线程,join是阻塞当前线程,即当前线程结束时不会退出。从结果可以看出,直到Thread-1结束,主线程才结束。

在Python中,默认情况下,如果不加join语句,主线程会等到当前线程结束后才结束,但不会立即kill该线程。如果没有加入join,输出如下:

the curent threading  MainThread is running
the curent threading  Thread-1 is running
the curent threading  MainThread is ended
the curent threading  Thread-1 is ended

但是如果在线程实例中添加t.setDaemon(True),如果不添加join语句,那么当主线程结束时,子线程就会被杀死。

代码:

import threading
import time
def target():
    print 'the curent threading  %s is running' % threading.current_thread().name
    time.sleep(4)
    print 'the curent threading  %s is ended' % threading.current_thread().name
print 'the curent threading  %s is running' % threading.current_thread().name
t = threading.Thread(target=target)
t.setDaemon(True)
t.start()
t.join()
print 'the curent threading  %s is ended' % threading.current_thread().name

输出如下:

the curent threading  MainThread is running
the curent threading  Thread-1 is runningthe curent threading  MainThread is ended

如果加入join并设置等待时间,它会在退出前等待线程一段时间:

import threading
import time
def target():
    print 'the curent threading  %s is running' % threading.current_thread().name
    time.sleep(4)
    print 'the curent threading  %s is ended' % threading.current_thread().name
print 'the curent threading  %s is running' % threading.current_thread().name
t = threading.Thread(target=target)
t.setDaemon(True)
t.start()
t.join(1)

输出:

the curent threading  MainThread is running
the curent threading  Thread-1 is running
the curent threading  MainThread is ended

主线程等待1秒后自动结束,杀死子线程。如果join不加等待时间,t.join()会一直等到子线程结束,输出如下:

the curent threading  MainThread is running
the curent threading  Thread-1 is running
the curent threading  Thread-1 is ended
the curent threading  MainThread is ended

本文为原创文章,版权归所有,欢迎分享本文,转载请保留出处!

知行编程网
知行编程网 关注:1    粉丝:1
这个人很懒,什么都没写
扫一扫二维码分享