0

I am running two producer and one consumer thread. The producer threads enqueue packets in to the queue and the consumer thread dequeues them. Even though I join all the threads, not all complete. I am wondering why. Below is my code: Note both print statements 'finishing consumer thread\n', and 'exiting the program' never get executed.

def PacketConsumer(q, numTicks):  #dequeues and enqueues and returns the packet
    while True:
        if not q.empty():
            print('qsize=', q.qsize(), '\n')
            pkt = q.get()
            q.task_done()
            print('dequeuing Packet', ' ', 'thread=', pkt.srcID, ' ', pkt.pktId, '\n')
            time.sleep(0.01)
    print('finishing consumer thread\n')
    return


if __name__ == '__main__':
    numTicks = 40
    q1 = Queue(maxsize=5)
    x = threading.Thread(target=PacketProducer, args=(1, numTicks, 0.7, 0.7, q1))
    y = threading.Thread(target=PacketProducer, args=(2, numTicks, 0.6, 0.4, q1))
    t = threading.Thread(target=PacketConsumer, args=(q1,numTicks))
    t.start()
    x.start()
    y.start()
    t.join()
    x.join()
    y.join()
    print('exiting the program')
ijlalShah
  • 43
  • 3

0 Answers0