fork download
  1. import multiprocessing as mp
  2. import sys
  3. import time
  4.  
  5. num = mp.Value('d', 0.0, lock = True)
  6.  
  7.  
  8. def func1():
  9. global num
  10. print ('start func1')
  11. #While num.value < 100000:
  12. for x in range(1000):
  13. num.value += 1
  14. #print(num.value)
  15. print ('end func1')
  16.  
  17. def func2():
  18. global num
  19. print ('start func2')
  20. #while num.value > -10000:
  21. for x in range(1000):
  22. num.value -= 1
  23. #print(num.value)
  24. print ('end func2')
  25.  
  26. if __name__=='__main__':
  27. ctx = mp.get_context('fork')
  28. p1 = ctx.Process(target=func1)
  29. p1.start()
  30. p2 = ctx.Process(target=func2)
  31. p2.start()
  32. p1.join()
  33. p2.join()
  34. sys.stdout.flush()
  35. print(num.value)
Success #stdin #stdout 0.07s 54776KB
stdin
Standard input is empty
stdout
start func2
end func2
start func1
end func1
0.0