Skip to content

Instantly share code, notes, and snippets.

@JosephRedfern
Created September 5, 2017 13:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JosephRedfern/110be0479aaf8c986569bb23aef43ba5 to your computer and use it in GitHub Desktop.
Save JosephRedfern/110be0479aaf8c986569bb23aef43ba5 to your computer and use it in GitHub Desktop.
produces "TypeError: can't pickle _thread.lock objects" under W10 w/ Python 3.6.2.
import time
import random
from queue import Queue
from multiprocessing import Process
import os
def produce_data(q):
print("[+] Producer thread started (PID: {})".format(os.getpid()))
while True:
duration = random.randint(0, 1000)
value = random.randint(0, 1000)
time.sleep(duration/1000.0)
q.put(value)
def process_data(q):
print("[+] Processor thread started (PID: {})".format(os.getpid()))
while True:
data = q.get()
print("{} -- that's numberwang!".format(data))
if __name__ == '__main__':
q = Queue()
dp_proc = Process(target=produce_data, args=(q,))
pd_proc = Process(target=process_data, args=(q,))
dp_proc.start()
pd_proc.start()
# blocks forever
dp_proc.join()
pd_proc.join()
@JosephRedfern
Copy link
Author

Oops. Should be using multiprocessing.Queue not queue.Queue!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment