Python多进程

古城微笑少年丶 2023-01-17 07:47 90阅读 0赞

安装:

  1. pip install multiprocess

使用:

  1. import multiprocessing

创建进程:

  1. multiprocessing.Process([group [, target [, name [, args [, kwargs]]]]])
  2. # target表示调用对象
  3. # args表示调用对象的位置参数元组
  4. # kwargs表示调用对象的字典
  5. # name为别名
  6. # group实质上不使用

方法:
is_alive() :进程是否存活
join([timeout])
run()
start() :启动一个进程
terminate()

属性:authkey、daemon(要通过start()设置),exitcode(进程在运行时为None、如果为–N,表示被信号N结束)、name、pid。其中daemon是父进程终止后自动终止,且自己不能产生新进程,必须在start()之前设置。

创建一个单进程:

  1. import multiprocessing
  2. import time
  3. def func():
  4. for i in range(5):
  5. print(i,"*"*10)
  6. time.sleep(2)
  7. if __name__ == '__main__':
  8. p=multiprocessing.Process(target=func)
  9. p.start()
  10. print("p.pid:",p.pid)
  11. print("p.name",p.name)
  12. print("p.is_alive",p.is_alive())
  13. # p.pid: 11792
  14. # p.name Process-1
  15. # p.is_alive True
  16. # 0 **********
  17. # 1 **********
  18. # 2 **********
  19. # 3 **********
  20. # 4 **********

2.创建函数并将其作为多进程

  1. import multiprocessing
  2. import time
  3. def func1():
  4. for i in range(5):
  5. print(i,"*"*10)
  6. time.sleep(2)
  7. def func2():
  8. for i in range(5):
  9. print(i*10,"*"*10)
  10. time.sleep(2)
  11. if __name__ == '__main__':
  12. p1=multiprocessing.Process(target=func1)
  13. p1.start()
  14. p2 = multiprocessing.Process(target=func2)
  15. p2.start()
  16. print("The number of CPU is:" + str(multiprocessing.cpu_count()))
  17. # The number of CPU is:2
  18. # 0 **********
  19. # 0 **********
  20. # 1 **********
  21. # 10 **********
  22. # 2 **********
  23. # 20 **********
  24. # 3 **********
  25. # 30 **********
  26. # 4 **********
  27. # 40 **********

将进程定义为类

  1. import multiprocessing
  2. import time
  3. class Mytest(multiprocessing.Process):
  4. def __init__(self,interval):
  5. multiprocessing.Process.__init__(self)
  6. self.interval=interval
  7. def run(self):
  8. n = 5
  9. while n > 0:
  10. print("the time is {0}".format(time.ctime()))
  11. time.sleep(self.interval)
  12. n -= 1
  13. if __name__ == '__main__':
  14. p=Mytest(2)
  15. p.start()
  16. # the time is Tue Apr 27 20:31:10 2021
  17. # the time is Tue Apr 27 20:31:12 2021
  18. # the time is Tue Apr 27 20:31:14 2021
  19. # the time is Tue Apr 27 20:31:16 2021
  20. # the time is Tue Apr 27 20:31:18 2021

设置daemon属性,主进程结束,子进程就随着结束

不加daemon:

  1. import multiprocessing
  2. import time
  3. def func(sle):
  4. print("time now_1:{}".format(time.ctime()))
  5. time.sleep(sle)
  6. print("time now_2:{}".format(time.ctime()))
  7. if __name__ == '__main__':
  8. p=multiprocessing.Process(target=func,args=(2,))
  9. p.start()
  10. print("end!")
  11. # end!
  12. # time now_1:Tue May 18 16:52:22 2021
  13. # time now_2:Tue May 18 16:52:24 2021

加上daemon:

  1. import multiprocessing
  2. import time
  3. def func(sle):
  4. print("time now_1:{}".format(time.ctime()))
  5. time.sleep(sle)
  6. print("time now_2:{}".format(time.ctime()))
  7. if __name__ == '__main__':
  8. p=multiprocessing.Process(target=func,args=(2,))
  9. p.daemon=True
  10. p.start()
  11. print("end!")
  12. # end!

join让其中一个子进程运行,在这个子进程运行完毕,在让另外一个子进程运行,所以在其中一个子进程运行期间,其他进程只能按着固有的顺序等待。

  1. import multiprocessing
  2. import time
  3. def funcA():
  4. sle=2
  5. print("funcA_time now_1:{}".format(time.ctime()))
  6. time.sleep(sle)
  7. print("funcA_time now_2:{}".format(time.ctime()))
  8. return sle
  9. def funcB():
  10. t=funcA()
  11. print("funcB_time now_1:{}".format(time.ctime()))
  12. time.sleep(t)
  13. print("funcB_time now_2:{}".format(time.ctime()))
  14. if __name__ == '__main__':
  15. p1=multiprocessing.Process(target=funcA)
  16. p2=multiprocessing.Process(target=funcB)
  17. p1.start()
  18. p1.join()
  19. p2.start()
  20. p2.join()
  21. print("end!")
  22. #
  23. # funcA_time now_1:Tue May 18 17:12:28 2021
  24. # funcA_time now_2:Tue May 18 17:12:30 2021
  25. # funcA_time now_1:Tue May 18 17:12:30 2021
  26. # funcA_time now_2:Tue May 18 17:12:32 2021
  27. # funcB_time now_1:Tue May 18 17:12:32 2021
  28. # funcB_time now_2:Tue May 18 17:12:34 2021
  29. # end!

互斥锁
加锁可以保证多个进程修改同一块数据时,同一时间只能有一个任务可以进行修改,即串行的修改

  1. from multiprocessing import Lock,Process
  2. import time
  3. import os
  4. def funcA(mutex):
  5. sle=2
  6. print("funcA_time now_1:{} pid:{}".format(time.ctime(),os.getpid()))
  7. time.sleep(sle)
  8. print("funcA_time now_2:{} pid:{}".format(time.ctime(),os.getpid()))
  9. return sle
  10. def funcB(mutex):
  11. t=funcA(mutex)
  12. print("funcB_time now_1:{}".format(time.ctime()))
  13. time.sleep(t)
  14. print("funcB_time now_2:{}".format(time.ctime()))
  15. if __name__ == '__main__':
  16. mutex=Lock()
  17. p1=Process(target=funcA,args=(mutex,))
  18. p2=Process(target=funcA,args=(mutex,))
  19. p1.start()
  20. print(p1.pid)
  21. time.sleep(10)
  22. p2.start()
  23. print(p2.pid)
  24. print("end!")
  25. # 7292
  26. # funcA_time now_1:Tue May 18 17:23:48 2021 pid:7292
  27. # funcA_time now_2:Tue May 18 17:23:50 2021 pid:7292
  28. # 20396
  29. # end!
  30. # funcA_time now_1:Tue May 18 17:23:58 2021 pid:20396
  31. # funcA_time now_2:Tue May 18 17:24:00 2021 pid:20396

发表评论

表情:
评论列表 (有 0 条评论,90人围观)

还没有评论,来说两句吧...

相关阅读

    相关 python 进程

    时间有限: 后面再补充 总结一下就是,多任务的实现有3种方式:多进程模式;多线程模式;多进程+多线程模式。 python中多进程 就是调用模块: multiprocessi

    相关 Python进程进程

    由于Python中线程封锁机制,导致Python中的多线程并不是正真意义上的多线程。当我们有并行处理需求的时候,可以采用多进程迂回地解决。 如果要在主进程中启动大量的子进程,

    相关 Python进程

    Python多进程方面涉及到的模块主要包括: subprocess: 可以在当前程序中执行其他程序或命令 mmap:提供一种基于内存的进程间通信机制 mul