import os import time from concurrent.futures.process import ProcessPoolExecutor from multiprocessing import Manager
defjob(v, k): for i in range(10): value = v.setdefault(k, 0) time.sleep(0.1) v[k] = value + i print(f"{os.getpid()}{value}{v}")
if __name__ == '__main__': v = Manager().dict() jobs = ["aa", "bb"] p = ProcessPoolExecutor(max_workers=2) for i in range(10): for k in jobs: p.submit(job, v, k)
import os import time from concurrent.futures.process import ProcessPoolExecutor from multiprocessing import Manager
lock = Manager().Lock()
defjob(v, k): lock.acquire() for i in range(10): value = v.setdefault(k, 0) time.sleep(0.1) v[k] = value + i print(f"{os.getpid()}{value}{v}") lock.release()
if __name__ == '__main__': v = Manager().dict() jobs = ["aa", "bb"] p = ProcessPoolExecutor(max_workers=5) for i in range(10): for k in jobs: p.submit(job, v, k)
import os import time from concurrent.futures.process import ProcessPoolExecutor from multiprocessing import Manager
defjob(v, k, lock): with lock: for i in range(10): value = v.setdefault(k, 0) time.sleep(0.1) v[k] = value + i print(f"{k}{os.getpid()}{value}{v}")
if __name__ == '__main__': v = Manager().dict() l = {} jobs = ["aa", "bb"] p = ProcessPoolExecutor(max_workers=5) for i in range(10): for k in jobs: lock = l.setdefault(k, Manager().Lock()) p.submit(job, v, k, lock)
import time from concurrent.futures.process import ProcessPoolExecutor from dataclasses import dataclass from multiprocessing import Manager
@dataclass classItem: H: int = 1 def ChangeItem(v, item, i): try: a = v[i].H time.sleep(0.1) t = Item() t.H = a + i v[i] = t print(v) except Exception as e: print(e) if __name__ == '__main__': v = Manager().dict() p = ProcessPoolExecutor(max_workers=5) for i in range(10): item = v.setdefault(i, Item()) p.submit(ChangeItem, v, item, i) for i in range(10): item = v.setdefault(i, Item()) p.submit(ChangeItem, v, item, i)