linux才是真正的多用户多任务
多任务,一般是通过时间片轮转和优先级调度等实现并发
进程
正在运行着的代码
线程
fork
import os
import time
ret = os.fork()
if ret == 0:
while True:
print("xjxjxjxjjx")
time.sleep(1)
else:
while True:
print("22222222")
time.sleep(1)
xjxjxjxjjx
xjxjxjxjjx
22222222
xjxjxjxjjx
22222222
xjxjxjxjjx
22222222
xjxjxjxjjx
22222222
getpid返回当前进程标识,getppid返回父进程标识。父进程中fork的返回值就是刚刚创建的子进程的id
import os
ret = os.fork()
print(ret)
if ret > 0:
print("父进程---%d"%os.getpid())
else:
print("子进程---%d %d"%(os.getpid(),os.getppid()))
-----------------------------------------------------------------------
23644
父进程---23643
0
子进程---23644 23643
父进程和子进程的执行顺序
import os
import time
ret = os.fork()
if ret == 0:
print("子进程1111")
time.sleep(5)
print("子进程2---")
else:
print("父进程---")
time.sleep(3)
print("------over------")
-------------------------------------------------
父进程---
子进程1111
------over------
EPOQUEs-MacBook-Pro:plane epoque$ 子进程2---
------over------
多进程修改全局变量
进程之间不会共享数据
import os
import time
num = 100
ret = os.fork()
if ret == 0:
print("111111111")
num += 1
print("子进程%d"%num)
else:
time.sleep(3)
print("22222222")
print("父进程%d"%num)
——————————————————————————
111111111
子进程101
22222222
父进程100
多个进程的运行顺序
import os
import time
ret = os.fork()
if ret == 0:
print("111111111")
else:
print("22222222")
ret = os.fork()
if ret == 0:
print("333333333333")
else:
print("44444444444")
______________________________
22222222
44444444444
111111111
333333333333
44444444444
333333333333
fork不夸平台,只能在linux上使用,所以一般用Process,这个主进程会等所有子进程运行完主进程才能结束,此处和fork不一样。所以,一般不用fork
from multiprocessing import Process
import time
def test():
while True:
print("-----test-----")
time.sleep(1)
p = Process(target=test)
p.start()
while True:
print("main")
time.sleep(1)
————————————————————————————
main
-----test-----
-----test-----
main
-----test-----
main
-----test-----
main
-----test-----
main
-----test-----
……
jion(timeout)子进程
等到标记的进程结束之后,才会往下走(等待时间)。
from multiprocessing import Process
import time
import random
def test():
for i in range(random.randint(1,5)):
print("-----test-----%d"%i)
time.sleep(1)
p = Process(target=test)
p.start()
p.join() #堵塞
print("main")
————————————————————————————————
-----test-----0
-----test-----1
-----test-----2
-----test-----3
main
进程的创建——Process子类
from multiprocessing import Process
import time
class MyNewProcess(Process):
def run(self):
while True:
print("----1-----")
time.sleep(1)
p = MyNewProcess()
p.start()
while True:
print("-----main_--___")
time.sleep(1)
——————————————————————————————————
-----main_--___
----1-----
----1-----
-----main_--___
----1-----
-----main_--___
----1-----
-----main_--___
----1-----
……
进程池
如果添加的任务数量超过了进程池的进程个数的话,那么不会导致添加不进入添加到进程中的任务,如果还没有被执行的话,那么此时,他们会等待进程池中的进程完成一个任务后,会自动去用刚刚那个进程,完成新的任务
from multiprocessing import Pool
import time
import random
import os
def worker():
for i in range(5):
print("_______pid=%d i=%d"%(os.getpid(),i))
time.sleep(1)
#表示进程池中最多有三个进程
pool = Pool(3)
for i in range(10):
print("%d"%i)
pool.apply_async(worker)
#关闭进程池,就不能再添加新的任务
pool.close()
#如果没有join,会导致进程池中的任务不会执行
pool.join()
堵塞就相当于是单线程了
pool.apply(woker,(i,))
进程间通信-Queue
from multiprocessing import Queue
q = Queue()
q.empty()
q.full()
q.get()
多进程copy文件
from multiprocessing import Pool,Manager
import os
def copyFileTask(name, oldFolderName, newFolderName,queue):
#文件的copy功能
fr = open(oldFolderName +"/"+name)
fw = open(newFolderName+"/"+name,"w")
content = fr.read()
fw.write(content)
fr.close()
fw.close()
queue.put(name)
def main():
#1、创建一个文件夹
oldFolderName = input("输入文件夹的名字:")
newFolderName = oldFolderName + "_复件"
os.mkdir(newFolderName)
#2、获取old文件夹中的所有文件名
fileNames = os.listdir(oldFolderName)
print(fileNames)
#3、使用多进程copy,源文件夹中的所有文件到新文件夹中
pool = Pool(5)
queue = Manager().Queue()
for name in fileNames:
pool.apply_async(copyFileTask,args=(name, oldFolderName, newFolderName,queue))
num = 0
allNum = len(fileNames)
while num<allNum:
queue.get()
num+=1
copyrate = (num/allNum)*100
print("进度%.2f%%"%copyrate)
if __name__ == "__main__":
main()