Created
January 31, 2020 12:01
-
-
Save samson-wang/2410957e2670c473037a8f54a3f3190b to your computer and use it in GitHub Desktop.
multi-processing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import multiprocessing as mp | |
import time | |
import random | |
todo = mp.Value('i', 0) | |
done = mp.Value('i', 0) | |
idx = mp.Value('i', 0) | |
finish = mp.Value('i', 0) | |
def worker(todo, done, idx, finish): | |
while True: | |
if finish.value: | |
break | |
if todo.value == 1: | |
tmp_idx = idx.value | |
print("Processing", tmp_idx) | |
time.sleep(0.5) | |
todo.value = 0 | |
done.value = 1 | |
else: | |
time.sleep(0.01) | |
if __name__ == "__main__": | |
p = mp.Process(target=worker, args=(todo, done, idx, finish)) | |
p.start() | |
fid = 0 | |
done.value = 1 | |
for i in range(200): | |
print(fid) | |
if done.value: | |
todo.value = 1 | |
idx.value = fid | |
done.value = 0 | |
fid += 1 | |
time.sleep(0.01) | |
finish.value = 1 | |
p.join() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment