进程的定义和常用方法
import os
from multiprocessing import Process
from time import sleepm = 1
list1 = []def task1(s, name):global mwhile True:sleep(1)m += 1print("进程1", m)list1.append(str(m) + "task1")print(list1)print("------------>1", "进程号:{},进程名字:{},父进程号:{}".format(os.getpid(), name, os.getppid()))def task2(s, name):while True:sleep(2)print("进程2", m)list1.append(str(m) + "task2")print(list1)print("------------>2", "进程号:{},进程名字:{},父进程号:{}".format(os.getpid(), name, os.getppid()))if __name__ == '__main__':print(os.getpid(), "进程号:{},父进程号:{}".format(os.getpid(), os.getppid()))p1 = Process(target=task1, name="任务1", args=(2, "小明"))p1.start()print(p1.name)p2 = Process(target=task2, name="任务2", args=(1, "小尼"))p2.start()print(p2.name)print("~~~~~~~~~~~~~~~~~~~~~~~")
自定义进程
from multiprocessing import Process
from time import sleepclass MyPro(Process):n = 1def __init__(self, name):super().__init__()self.name = namedef run(self):global nwhile True:print("{},自定义进程{}".format(self.name, self.n))self.n += 1if __name__ == '__main__':pro = MyPro("小明")pro.start()
进程池(task,args)--非阻塞式
设置进程数量,进程复用
非阻塞式:
直接把任务添加给进程,不需要等待
import os
import time
from multiprocessing import Pool
from random import randomdef task(task_name):print("开始做任务了:{}".format(task_name))start = time.time()time.sleep(random() * 2)end = time.time()print("任务{}用时:{},______>id:{}".format(task_name, (end - start), os.getpid()))if __name__ == '__main__':pool = Pool(5)list1 = ["听音乐", "跑步", "看孩子", "打游戏", "散步", "洗衣服"]for i in list1:pool.apply_async(task, args=(i,))pool.close()pool.join()print("over~~~~~~~~~~~~~~~~~~~")
进程池(task,args,callback)
import os
import time
from multiprocessing import Pool
from random import randomdef task(task_name):print("开始做任务了:{}".format(task_name))start = time.time()time.sleep(random() * 2)end = time.time()return "任务{}用时:{},______>id:{}".format(task_name, (end - start), os.getpid())con = []def func(n):con.append(n)if __name__ == '__main__':pool = Pool(5)list1 = ["听音乐", "跑步", "看孩子", "打游戏", "散步", "洗衣服"]for i in list1:pool.apply_async(task, args=(i,), callback=func)pool.close()pool.join()for i in con:print(i)print("over~~~~~~~~~~~~~~~~~~~")
进程池(task,args)--阻塞式
import os
import time
from multiprocessing import Pool
from random import randomcon = []def task(task_name):print("{}---开始做任务,进程Id:{}".format(task_name, os.getpid()))start = time.time()time.sleep(random() * 2)end = time.time()print("{}任务结束了,用时{}".format(task_name, (end - start)))if __name__ == '__main__':pool = Pool(5)list1 = ["洗衣服", "吃饭", "打豆豆", "喝酒", "喝饮料", "跑步"]for ll in list1:pool.apply(task, args=(ll,))pool.close()pool.join()print("---------------over")
进程间的通信
from multiprocessing import Queue
from multiprocessing import Process
from time import sleepdef down_load(queue):images = ['girl.jpg', 'boy.jpg', 'man.jpg']for image in images:print("正在下载{}".format(image))sleep(0.5)queue.put(image)def getfile(queue):while True:try:file = queue.get(timeout=5)print("{}文件保存成功!".format(file))except:breakif __name__ == '__main__':queue = Queue(5)p1 = Process(target=down_load, args=(queue,))p2 = Process(target=getfile, args=(queue,))p1.start()p2.start()