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

一、线程概念
在一个过程中,至少有一个控制线程。过程的概念只是一个抽象的概念。CPU上真正调度的是过程中的线程,就像地铁过程中真正工作的线程实际上是地铁中的线程一样。北京地铁至少应该有一个线程。线程是真正工作的,线程使用过程中包含的一堆资源。线程只是一个调度单元,不包括资源。
何时需要打开多个线程:一个过程中的多个线程共享该过程中的资源,因此如果多个任务共享相同的资源,则需要打开多个线程。 多线程是指在一个过程中打开多个线程。简单地说,如果多个任务共享相同的资源空间,则必须在一个过程中打开多个线程。一个过程可能对应于多个子任务。如果一个过程中只打开一个线程,则多个子任务实际上是串行执行效果,即一个过程中只包含一个执行路径。
对于计算密集型应用,应使用多个过程;对于IO密集型应用,应使用多线程。创建线程的成本远低于创建过程。
Python中线程的特点
1.在其他语言中,在一个过程中打开多个线程,每个线程都可以用于cpu,但在 在python中,在同一时刻,一个过程中只能有一个线程处于运行状态。
2.例如,在其他语言中,例如,我现在打开了一个包含多个线程的过程。如果我现在有多个CPU,每个线程都可以对应于相应的CPU。
3.但在python中,如果我们现在开始一个对应多个线程的过程,只有一个线程可以同时运行。 对于其他语言,在多CPU系统中,可以打开多个线程,以限制多核的使用。 但Python中的多线程不能利用多核优势。
4.在同一过程中,多个线程可以相互通信;然而,过程和过程之间的通信必须基于IPC 信息通信机制(IPC机制包括队列和管道)。在一个过程中,改变主线程可能会影响其他线程的行为,但改变父过程不会影响其他子过程,因为过程与过程完全隔离。 在python中,只有一个线程可以同时在同一时间和同一过程中运行。如果一个线程被系统调用和阻塞,整个过程将被悬挂。
三、多线程理解
多过程和多线程可以执行多个任务,线程是过程的一部分。线程的特点是线程可以共享内存和变量,资源消耗较少(但在Unix环境中,多过程和多线程资源调度消耗差距不明显,Unix调度较快),缺点是线程同步和锁更麻烦。
相关推荐:Python视频教程
四、Python多线程创建
在Python中,还可以实现多线程,有两个标准模块thread和threading,但我们主要使用更先进的threading模块。使用例子:
importthreading importtime deftarget(): print'thecurentthreading%sisrunning'%threading.current_thread().name time.sleep(1) print'thecurentthreading%sisended'%threading.current_thread().name print'thecurentthreading%sisrunning'%threading.current_thread().name t=threading.Thread(target=target) t.start() t.join() print'thecurentthreading%sisended'%threading.current_thread().name
输出:
thecurentthreadingMainThreadisrunning thecurentthreadingThread-1isrunning thecurentthreadingThread-1isended thecurentthreadingMainThreadisended
start是启动线程,join阻塞了当前线程,即使在当前线程结束时也不会退出。从结果可以看出,主线程直到thread-1结束。
在Python中,默认情况下,如果不添加join语句,主线程不会等到当前线程结束,但不会立即杀死线程。如果不添加join输出,如下:
thecurentthreadingMainThreadisrunning thecurentthreadingThread-1isrunning thecurentthreadingMainThreadisended thecurentthreadingThread-1isended
但是,如果将t添加到线程实例中.setDaemon(True)之后,如果不添加join语句,则当主线程结束时,子线程将被杀死。
代码:
importthreading importtime deftarget(): print'thecurentthreading%sisrunning'%threading.current_thread().name time.sleep(4) print'thecurentthreading%sisended'%threading.current_thread().name print'thecurentthreading%sisrunning'%threading.current_thread().name t=threading.Thread(target=target) t.setDaemon(True) t.start() t.join() print'thecurentthreading%sisended'%threading.current_thread().name
输出如下:
thecurentthreadingMainThreadisrunning thecurentthreadingThread-1isrunningthecurenthreadingMainThreadisediseding
若添加join并设置等待时间,则等待线程一段时间后退出:
importthreading importtime deftarget(): print'thecurentthreading%sisrunning'%threading.current_thread().name time.sleep(4) print'thecurentthreading%sisended'%threading.current_thread().name print'thecurentthreading%sisrunning'%threading.current_thread().name t=threading.Thread(target=target) t.setDaemon(True) t.start() t.join(1)
输出:
thecurentthreadingMainThreadisrunning thecurentthreadingThread-1isrunning thecurentthreadingMainThreadisended
主线程等待1秒,自动结束,杀死子线程。如果join没有额外的等待时间,t.join(),会一直等到子线程结束,输出如下:
thecurentthreadingMainThreadisrunning thecurentthreadingThread-1isrunning thecurentthreadingThread-1isended thecurentthreadingMainThreadisended
相关推荐:
Python中的多过程是什么?
