一、线程的概念

一个进程里面至少有一个控制线程,进程的概念只是一种抽象的概念,真正在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中的多进程是什么

python中线程里面多线程_Python中的线程和多线程是什么相关推荐

  1. python什么是多线程_python中多线程指的是什么

    python中多线程指的是什么 发布时间:2020-09-03 10:59:59 来源:亿速云 阅读:73 作者:小新 这篇文章主要介绍python中多线程指的是什么,文中介绍的非常详细,具有一定的参 ...

  2. python 协程是啥_python中的协程

    协程,又称微线程,纤程.英文名Coroutine. 协程是啥 协程是python个中另外一种实现多任务的方式,只不过比线程更小占用更小执行单元(理解为需要的资源). 为啥说它是一个执行单元,因为它自带 ...

  3. python怎么用q退出_python中退出

    广告关闭 腾讯云11.11云上盛惠 ,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元! for循环elsefor循环如果正常结束的时候,才会结束else语句#! usr ...

  4. python中函数的作用域_Python中的函数作用域

    在python中,一个函数就是一个作用域 name = 'xiaoyafei' def change_name(): name = '肖亚飞' print('在change_name里的name:', ...

  5. python列表怎么写文件_python中以字典为元素的列表怎么写入文本文件

    python如何将列表中的元素添加进字典纵然被命运的铁蹄狠狠践踏,也顽强地长出自己的根芽. 录入自己和另一个人的名字的汉语拼音简写,然后依据标识符中字母的数值两个人,一颗心,依偎的不是爱情而是那小温暖 ...

  6. python中赋值语句的作用_python中return可以使用赋值语句吗?

    在python中,有各种不同类型的语句.一个python程序是由模块构成的;一个模块由一条或多条语句组成;每个语句由不同的表达式组成;表达式可以创建和操作对象.下面来看看python中的语句. 赋值语 ...

  7. python中round函数参数_python中关于round函数的小坑

    round函数很简单,对浮点数进行近似取值,保留几位小数.比如 >>> round(10.0/3, 2) 3.33 >>> round(20/7) 3 第一个参数是 ...

  8. python里的join方法_python中join()方法介绍

    描述 Python join() 方法用于将序列中的元素以指定的字符连接生成一个新的字符串. 语法 join()方法语法: str . join ( sequence ) 参数 sequence -- ...

  9. python常用函数的用法_python中常用函数整理

    1.map map是python内置的高阶函数,它接收一个函数和一个列表,函数依次作用在列表的每个元素上,返回一个可迭代map对象. class map(object):""&qu ...

最新文章

  1. 个人微信公众号已开通,欢迎各位小伙伴关注
  2. Firefox 66正式向广告宣战!预设禁止自动播放影片并加入加入 Windows Hello
  3. 【Kotlin】扩展接收者 与 分发接收者 ( 类内部扩展用法 | 注意事项 | open 修饰扩展 )
  4. 并发之AQS原理(一) 原理介绍简单使用
  5. 为什么总说做产品经理的门槛低?
  6. 【Qt】Qt资源应用--菜单图标
  7. 【JFreeChart】JFreeChart—输出区域图
  8. 邮件联系人,如何恕不部分字母就能显示邮件联系人
  9. 重磅!谷歌Fuchsia操作系统将支持运行Linux应用程序
  10. vue+elementui 房贷计算器
  11. CSS3干货23:常用字体样式设置
  12. 聚观早报|饿了么星选停止运营;百度2022财报全年营收1236.75亿元
  13. 第1讲、Cadence Allergo绘制小马哥DragonFly四轴飞行器PCB四层板教程简介
  14. 多线程的终止方法(停止线程)
  15. 如何快速取消关注B站(哔哩哔哩)所有up主?(Javascript实现)
  16. Android Gradle是什么?
  17. 第12集 副词及其他
  18. 全面解析电商数据挖掘之关联算法
  19. ofo 共享单车的问题
  20. Android 锁屏键和home键分开处理

热门文章

  1. TransE算法解析
  2. python 保存文件 吃内存_python检测空间储存剩余大小和指定文件夹内存占用的实例...
  3. linux怎么进入优盘目录,LINUX 9.0怎么挂载U盘?
  4. python网络爬虫学习笔记(十一):Ajax数据爬取
  5. X86,X86_32,X86_64
  6. AD20更改PCB 2D视图的背景环境颜色
  7. UML--类之间的五种关系
  8. MFC——在状态栏中显示滚动字幕
  9. javascript中的||运算符
  10. 生活大爆炸系列之制作望远镜架