python之进程和线程
1 操作系统
为什么要有操作系统 ?
操作系统位于底层硬件与应用软件之间的一层
工作方式:向下管理硬件,向上提供接口
操作系统进程切换:
- 出现IO操作
- 固定时间
2 进程和线程的概念
进程就是一个程序在一个数据集上的一次动态执行过程。进程一般由程序,数据集和进程控制块三部分组成。
程序用来描述进程要完成哪些功能以及如何完成
数据集则是程序在执行过程中所需要使用的资源
进程控制块用来记录进程的外部特征,描述进程的执行变化过程
系统可以利用他来控制和管理进程,它是系统感知进程存在的唯一标志
线程也叫轻量级进程,它是一个基本的cpu执行单位,也是程序执行过程中的最小单元,由线程ID、程序计数器、寄存器集合和堆栈共同组成。
总结:进程是一个最小的资源管理过程(存放线程的容器),并不是一个实物,可以解决并发的问题
进程之间一定是的独立的
线程是一个最小的执行单位,所使用的数据是进程的,切换速度大于进程
进程和线程的关系:
一个线程只能属于一个进程,而一个进程可以有多个线程。
资源分配给进程,同一个进程的所有线程共享该进程的所有资源
CPU分给线程,即真正在CPU上运行的是线程
3 并行和并发
并行处理是计算机系统中能同时执行两个或更多个处理的一种计算方法。并行处理可同时工作于同一程序的不同方面。并行处理的主要目的是节省大型和复杂问题的解决问题。并行处理:指一个时间段中有几个程序都处于已启动运行到运行完毕之间,且这几个程序都是在同一个处理机上运行,但任一个时刻点上只有一个程序在处理机上运行。并发的关键是你有处理多个任务的能力,不一要同时。并行的关键是你有同时处理多个任务的能力。所以说,并行是并发的子集。
python中可以实现多进程的并发,不能实现多线程的并发
同步和异步:
同步就是一个指进程在执行某个请求的时候若该请求需要一段时间才能返回信息,那么这个进程将会一直等待下去,直到收到返回信息才能继续执行下去;
异步是一个指进程不需要一直等下去,而是继续执行下面的操作,不管其他进程的状态。当有消息返回时系统会通知进程进行处理,这样可以提高执行的效率。
4 threading模块
threading使用两种分方式
(1) 直接调用
import threading
import time
def tingge():
print("听歌")
time.sleep(3)
print("听歌结束")
def xieboke():
print("写博客")
time.sleep(5)
print("写博客结束") # tingge()
# xieboke()
t1=threading.Thread(target=tingge)#threading的一个类Thread
t2=threading.Thread(target=xieboke)#产生两个实例
t1.start()
t2.start()
print("ending!")
(2) 创建类
import threading
import time
class MyThread(threading.Thread):#创建一个类
def __init__(self,num):
threading.Thread.__init__(self)
self.num=num
def run(self):
print("running on number:%s" %self.num)
time.sleep(2)
t1=MyThread(56)
t2=MyThread(78)
t1.start()
t2.start()
print("ending!")
(1) join方法
import threading
from time import ctime,sleep
import time def Music(name): print ("Begin listening to {name}. {time}".format(name=name,time=ctime()))
sleep(3)
print("end listening {time}".format(time=ctime())) def Blog(title): print ("Begin recording the {title}. {time}".format(title=title,time=ctime()))
sleep(5)
print('end recording {time}'.format(time=ctime())) threads = [] t1 = threading.Thread(target=Music,args=('FILL ME',))#创建实例
t2 = threading.Thread(target=Blog,args=('python',)) threads.append(t1)#添加到列表
threads.append(t2) if __name__ == '__main__':
t1.start()#ti子线程开始运行
t1.join()#在t1子线程未完成时,父线程将一直被阻塞
t2.start()
t2.join() print ("all over %s" %ctime())
>>
Begin listening to FILL ME. Mon May 8 16:06:44 2017#运行t1,主线程被阻塞
end listening Mon May 8 16:06:47 2017#子线程运行完成,主线程运行
Begin recording the python. Mon May 8 16:06:47 2017#t2开始运行,主线程被阻塞
end recording Mon May 8 16:06:52 2017#子线程运行完成,主线程运行
all over Mon May 8 16:06:52 2017#主线程最后完成 (2)setdaemon方法
import threading
from time import ctime,sleep
import time def Music(name): print ("Begin listening to {name}. {time}".format(name=name,time=ctime()))
sleep(3)
print("end listening {time}".format(time=ctime())) def Blog(title): print ("Begin recording the {title}. {time}".format(title=title,time=ctime()))
sleep(5)
print('end recording {time}'.format(time=ctime())) threads = [] t1 = threading.Thread(target=Music,args=('FILL ME',))#创建实例
t2 = threading.Thread(target=Blog,args=('python',)) threads.append(t1)#添加到列表
threads.append(t2) if __name__ == '__main__': t2.setDaemon(True)#t2是守护进程 print ("all over %s" %ctime())
>>
Begin listening to FILL ME. Mon May 8 16:25:10 2017#t1子线程执行
Begin recording the python. Mon May 8 16:25:10 2017#t2子线程执行
all over Mon May 8 16:25:10 2017#主线程执行
end listening Mon May 8 16:25:13 2017#t1执行完成后主线程完成后t2也执行完成了。 import threading
from time import ctime,sleep
import time def Music(name): print ("Begin listening to {name}. {time}".format(name=name,time=ctime()))
sleep(3)
print("end listening {time}".format(time=ctime())) def Blog(title): print ("Begin recording the {title}. {time}".format(title=title,time=ctime()))
sleep(5)
print('end recording {time}'.format(time=ctime())) threads = [] t1 = threading.Thread(target=Music,args=('FILL ME',))#创建实例
t2 = threading.Thread(target=Blog,args=('python',)) threads.append(t1)#添加到列表
threads.append(t2) if __name__ == '__main__': t1.setDaemon(True)#t1是守护进程 for t in threads: t.start() print ("all over %s" %ctime())
>>
Begin listening to FILL ME. Mon May 8 16:32:24 2017#t1运行
Begin recording the python. Mon May 8 16:32:24 2017#t2运行
all over Mon May 8 16:32:24 2017#主线完成
end listening Mon May 8 16:32:27 2017
end recording Mon May 8 16:32:29 2017
python之进程和线程的更多相关文章
- Python的进程与线程--思维导图
Python的进程与线程--思维导图
- Python创建进程、线程的两种方式
代码创建进程和线程的两种方式 """ 定心丸:Python创建进程和线程的方式基本都是一致的,包括其中的调用方法等,学会一个 另一个自然也就会了. "" ...
- python之进程与线程
什么是操作系统 可能很多人都会说,我们平时装的windows7 windows10都是操作系统,没错,他们都是操作系统.还有没有其他的? 想想我们使用的手机,Google公司的Androi ...
- python的进程与线程(二)
线程 之前了解了操作系统的发展史,也知道了进程和线程的概念,归纳一下就是: 进程:本质上就是一段程序的运行过程(抽象的概念) 线程:最小的执行单元,是进程的实体 ...
- Python 9 进程,线程
本节内容 python GIL全局解释器锁 线程 进程 Python GIL(Global Interpreter Lock) In CPython, the global interpreter l ...
- python之进程和线程2
1 GIL全局解释器锁定义 定义:在一个线程拥有了解释器的访问权后,其他的所有线程都必须等待他释放解释器的访问权,即这些线程的下一条指令并不会互相影响. 缺点:多处理器退化为单处理器 优点:避免大量 ...
- 《Python》进程收尾线程初识
一.数据共享 from multiprocessing import Manager 把所有实现了数据共享的比较便捷的类都重新又封装了一遍,并且在原有的multiprocessing基础上增加了新的机 ...
- Python基础进程和线程
一 背景知识 进程的概念起源于操作系统,是操作系统最核心的概念. 进程是对正在运行程序的一个抽象,操作系统的其他所有内容都是围绕进程的概念展开的.所以想要真正了解进程,必须事先了解操作系统,egon介 ...
- Python中进程和线程的总体区别
Num01–>线程 线程是操作系统中能够进行运算调度的最小单位.它被包含在进程之中,是进程中的实际运作单位. 一个线程指的是进程中一个单一顺序的控制流. 一个进程中可以并发多条线程,每条线程并行 ...
随机推荐
- MVC异常处理(异常捕获)
1.cshtml页面异常 2.Controller异常 3.路由参数异常. 4.页面不存在404 页面不存在404,可以通过配置config来处理 <customErrors mode=&quo ...
- CTAP: Complementary Temporal Action Proposal Generation (ECCV2018)
互补时域动作提名生成 这里的互补是指actionness score grouping 和 sliding window ranking这两种方法提proposal的结合,这两种方法各有利弊,形成互补 ...
- python之 MySQLdb 实践 爬一爬号码
0.目录 2.构建URL3.新建数据库4.新建汇总表5.定义连接数据库函数:connect_db(db=None, cursorclass=DictCursor)6.汇总表填充必要数据7.新建各省份子 ...
- Azure附加新磁盘,差点掉进去的那个坑,注意临时数据盘
接今早的mysql问题,最终原因是mysql数据库的数据库文件以及pid丢失,当我还纳闷为什么丢失的情况下 我研究了下Azure云平台的数据磁盘原理,在Azure下,新建vm(centos)后只会提供 ...
- 一起学Hadoop——使用IDEA编写第一个MapReduce程序(Java和Python)
上一篇我们学习了MapReduce的原理,今天我们使用代码来加深对MapReduce原理的理解. wordcount是Hadoop入门的经典例子,我们也不能免俗,也使用这个例子作为学习Hadoop的第 ...
- root用户无法通过ssh连接Linux系统
ssh协议为了安全,有些版本默认禁止root用户的登陆 cd /etc/ssh 编辑sshd_config文件 cat sshd_config | grep PermitRootLogin Permi ...
- nacos-server集群 安装、运行(ubuntu)
下载.解压 wget -P /opt/downloads https://github.com/alibaba/nacos/releases/download/1.0.0/nacos-server-1 ...
- IDEA控制台问题:java lang OutOfMemoryError:PermGen space
PermGen space的全称是Permanent Generation space,是指内存的永久保存区域. OutOfMemoryError: PermGen space从表面上看就是内存溢出, ...
- 在jsp页面上方定义<style> 可以自定义class的样式
<style>.border-orange{ border:1px solid orange; width:120px; box-sizing: border-box; margin-bo ...
- Java集合—Set集和Map集
一.Set集合 1.概述 Set集合无序的.不可重复的元素(无序是指索引) Set集合不按照特定的方法进行排序,只是将元素放在集合中. 下面介绍一下Set集合的HashSet和TreeSet两个实现类 ...