Added multiprocessing testing

This commit is contained in:
2018-02-19 04:36:37 +01:00
parent 7685b8c6a8
commit e311c278e7
6 changed files with 181 additions and 8 deletions
+29
View File
@@ -0,0 +1,29 @@
import time
from multiprocessing import Process, Value, Lock
class Counter(object):
def __init__(self, initval=0):
self.val = Value('i', initval)
self.lock = Lock()
def increment(self):
with self.lock:
self.val.value += 1
def value(self):
with self.lock:
return self.val.value
def func(counter):
for i in range(50):
time.sleep(0.01)
counter.increment()
if __name__ == '__main__':
counter = Counter(0)
procs = [Process(target=func, args=(counter,)) for i in range(10)]
for p in procs: p.start()
for p in procs: p.join()
print (counter.value())