多进程虽然不允许多个进程同时修改同一份数据,但是多进程也有锁,为了在屏幕上打印的时候不出现两个进程同时执行的显示错误

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"

from multiprocessing import Process, Lock

def Child_Process(l, i):
    l.acquire()
    print('hello world', i)
    l.release()


if __name__ == '__main__':
    lock = Lock()  #声明一个进程锁

    for num in range(100):
        Process(target=Child_Process, args=(lock, num)).start()

 运行结果

Python多进程-进程锁

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-07-26
  • 2018-06-08
  • 2022-03-03
  • 2022-12-23
  • 2021-11-16
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-28
  • 2022-12-23
  • 2022-12-23
  • 2021-10-17
  • 2022-12-23
相关资源
相似解决方案