韩漫免费漫画在线观看方法,《好好疼爱里面》免费看,年轻漂亮的女士护士内衣,妈妈醉酒后把我当爸爸电视剧

文章 > Python高级 > Python中的线程和多线程是什么

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

头像

爱喝马黛茶的安东尼

2019-06-29 13:28:033169浏览 · 0收藏 · 0评论

一、线程的概念 

一个进程里面至少有一个控制线程,进程的概念只是一种抽象的概念,真正在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模块。使用例子:

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语句,那么主线程不会等到当前线程结束才结束,但却不会立即杀死该线程。如不加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

相关推荐:

Python中的多进程是什么

关注

关注公众号,随时随地在线学习

本教程部分素材来源于网络,版权问题联系站长!

男生困困进女生困困洞视频| 《再来一次好吗》免费观看| 《慈母夜吟》完整版| 蜜糖在线观看免费高清电视剧| 香港沦陷| 《伦敦空姐美版2023》| 免费观看已满十八岁电视剧高清版| 日本WINDOWSSERVER...| 《男医生的特殊治疗》| 狗配人的大片视频大全| 老公边日边叫我小骚B| 昊梦梦主人请好好疼爱里面动画第二季| 45岁老阿姨喷了三次尿素乳育| 免费观看已满十八岁的电视剧大全| 吻戏| 单亲妈妈用性缓解孩子压力| 《太太你想丈夫被开除吧》 | 暴躁少女CSGO高清大图特点| 妈妈醉酒后把我当爸爸电视剧| 无法抗拒的你ID中字| 少妇高潮喷水久久久久久久久 | 瑜伽馆里的私密按摩效果怎么样| 疯狂祖母免费观看高清版| 伦理《少妇的滋味》完整版| 松坂庆子| 《性房纵欲》在线观看| 乱亲H女乱秽XXXX| 共享女儿小诗1~10章的背景资.| 女性扒开大腿内侧小肚子痒| 偷偷在线观看免费的电视剧最新| 内谢中国媳妇最经典十句话| 小寡妇在线观看免费播放电视剧 | 沈倦| 蜜桃成熟时在线观看| 和妈妈做了怎么办心理咨询| 高压监狱2法国1时43分| 50多岁熟妇泻火原因处理方法 | 二人努力生猴子免费观看| 《交换做爰》在线观看| 舌绕指探洞深深下一句是什么?| 国产少女免费观看电视剧大全