luoson1 发表于 2018-8-13 13:15:36

Python的multiprocessing模块详解

import multiprocessing  
import time
  

  
def worker_with(lock,file):
  
    with lock:
  
      f=open(file,'a')
  
      f.write('Lock acquired via with')
  
      f.close()
  

  
def woker_no_with(lock,file):
  
    lock.acquire()
  
    try:
  
      f=open('file','a')
  
      f.write('Lock acquired directly')
  
      f.close()
  
    finally:
  
      lock.release()
  

  
if __name__ == '__main__':
  
    l=multiprocessing.Lock()
  
    w=multiprocessing.Process(target=worker_with,args=(l,'c:\\test1.txt'))
  
    nw=multiprocessing.Process(target=woker_no_with,args=(l,'c:\\test1.txt'))
  
    w.start()
  
    nw.start()
  
    w.join()
  
    nw.join()
页: [1]
查看完整版本: Python的multiprocessing模块详解