from multiprocessing import Process,Value import time import random
def save_money(money): for i in range(100): time.sleep(0.1) money.value += random.randint(1,200)
def take_money(money): for i in range(100): time.sleep(0.1) money.value -= random.randint(1,150)
money = Value('i',2000)
d = Process(target=save_money,args=(money,))#这里面money是全局的,不写也可 d.start() w = Process(target=take_money,args=(money,))#这里面money是全局的,不写也可 w.start()
d.join() w.join()
print(money.value)
补充:数据类型
Type code C Type Python Type Minimum size in bytes Notes 'b' signed char int 1 'B' unsigned char int 1 'u' Py_UNICODE Unicode character 2 (1) 'h' signed short int 2 'H' unsigned short int 2 'i' signed int int 2 'I' unsigned int int 2 'l' signed long int 4 'L' unsigned long int 4 'q' signed long long int 8 (2) 'Q' unsigned long long int 8 (2) 'f' float float 4 'd' double float 8