Skip to content

Instantly share code, notes, and snippets.

@kunalb
Last active May 5, 2024 19:15
Show Gist options
  • Save kunalb/17d825e3c961ffa2f3b20c5493fe77d5 to your computer and use it in GitHub Desktop.
Save kunalb/17d825e3c961ffa2f3b20c5493fe77d5 to your computer and use it in GitHub Desktop.
Values put into a multiprocessing queue are not safe
import multiprocessing as mp
def target(q):
while val := q.get():
print(len(val))
if __name__ == "__main__":
mp.set_start_method('forkserver')
q = mp.Queue()
p = mp.Process(target=target, args=(q,))
mutable = ["x"]
for i in range(10):
q.put(mutable)
for i in range(10):
mutable.append(i)
q.put(None)
p.start()
p.join()
for i in $(seq 40); do python3 mpq.py; echo; done;
1,11,11,11,11,11,11,11,11,11,
1,1,1,1,1,1,1,1,11,11,
1,1,1,1,1,1,1,1,11,11,
1,1,1,1,1,1,1,1,1,11,
1,11,11,11,11,11,11,11,11,11,
1,11,11,11,11,11,11,11,11,11,
1,11,11,11,11,11,11,11,11,11,
1,11,11,11,11,11,11,11,11,11,
1,1,1,1,1,1,1,1,1,11,
1,11,11,11,11,11,11,11,11,11,
1,1,1,1,1,1,1,1,1,11,
1,1,1,1,1,1,1,11,11,11,
1,11,11,11,11,11,11,11,11,11,
1,1,1,1,1,1,1,1,11,11,
1,11,11,11,11,11,11,11,11,11,
1,1,1,1,1,1,1,1,11,11,
1,1,1,1,1,1,1,11,11,11,
1,1,1,1,1,1,1,1,1,11,
1,1,1,1,1,1,1,1,1,11,
1,11,11,11,11,11,11,11,11,11,
1,11,11,11,11,11,11,11,11,11,
1,1,1,1,1,1,1,11,11,11,
1,1,1,1,1,1,1,1,1,11,
1,1,1,1,1,1,1,11,11,11,
1,1,1,1,1,1,1,1,11,11,
1,1,1,1,1,1,1,1,1,11,
1,1,1,1,1,1,1,11,11,11,
1,1,1,1,1,1,1,1,11,11,
1,11,11,11,11,11,11,11,11,11,
1,1,1,1,1,1,1,1,11,11,
1,1,1,1,1,1,1,1,1,11,
1,1,1,1,1,1,1,11,11,11,
1,1,1,1,1,1,1,1,11,11,
1,1,1,1,1,1,1,1,1,11,
1,11,11,11,11,11,11,11,11,11,
11,11,11,11,11,11,11,11,11,11,
1,1,1,1,1,1,1,1,1,11,
1,1,1,1,1,1,1,11,11,11,
1,11,11,11,11,11,11,11,11,11,
1,1,1,1,1,1,1,1,11,11,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment