Skip to content

Instantly share code, notes, and snippets.

@samson-wang
Created January 31, 2020 12:01
Show Gist options
  • Save samson-wang/2410957e2670c473037a8f54a3f3190b to your computer and use it in GitHub Desktop.
Save samson-wang/2410957e2670c473037a8f54a3f3190b to your computer and use it in GitHub Desktop.
multi-processing
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