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为共享内存对象,给他一个初始值2000,类型为正型“i”
相当于开辟了一个空间,同时绑定值2000,
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