简体   繁体   中英

One thread writing to queue, one thread reading

I'm trying to get one thread to add items to a queue and the main thread to pull them off. The approach I'm trying, having read the multiprocessing docs isn't working. What am I doing wrong? Thank you.

import time
from multiprocessing import Process, Queue

def append_to_queue(q, t=1):
    n = 0
    while True:
        q.put(n)
        n += 1
        time.sleep(t)

def print_queue(q, t=0.5):
    while True:
        print q.get()
        time.sleep(t)

def main(t1, t2, delay=1):
    q = Queue()
    p = Process(target=append_to_queue, args=(q, t1,))
    p.start()
    time.sleep(delay)
    print_queue(q, t2)
    p.join()

main(1, 0.5, delay=1)
  1. You're using processes not threads
  2. You're actually using a single process for producing but you only consume once in your main process. I think you want a consume process.

Here is a demo:

import time
from multiprocessing import Process, Queue, Event

def append_to_queue(t, q, stopnow):
    n = 0
    while not stopnow.is_set():
        q.put(n)
        n += 1
        time.sleep(t)
    q.put("producer done") # consumer will print this

def print_from_queue(t, q, stopnow):
    while not stopnow.is_set():
        print q.get()
        time.sleep(t)
    # drain queue
    for msg in xrange(q.qsize()):
        print msg
    print "consumer done"

def main(t1, t2):
    # @eryksun:
    # Because windows doesn't fork - the Queue
    # and Event can't be inherited from the main process.
    # Create them in main and pass them in the args tuple.
    q = Queue()
    stopnow = Event()
    producer = Process(target=append_to_queue, args=(t1, q, stopnow))
    producer.start()
    consumer = Process(target=print_from_queue, args=(t2, q, stopnow,))
    consumer.start()
    time.sleep(5)
    stopnow.set()
    producer.join()
    consumer.join()

# @eryksun:
# Windows doesn't fork, so you need to use if __name__ == '__main__'
# to guard calling main
if "__main__" == __name__:
    main(1, 0.5)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM