python之进程和线程

1 操作系统

为什么要有操作系统 ?

操作系统位于底层硬件与应用软件之间的一层

工作方式:向下管理硬件,向上提供接口

操作系统进程切换:

  1. 出现IO操作
  2. 固定时间

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

  

原文地址:https://www.cnblogs.com/asaka/p/6846945.html