ijlalShah
ijlalShah

Reputation: 43

dequeue function in python

I am using threads. One thread as a producer thread and the other thread as the consumer thread. The threads enqueue and dequeue from the same queue (q1). The producer function seems to be working fine. However, the consumer thread (dequeue function) is giving me error. Below is my code

def PacketProducer(Threadnum, numTicks, onProb, offProb, q):
   l=0
   onState = True
   packetId = 0
   for i in range(numTicks):
      x = generator(onProb, offProb, 'OnOff', onState)
      if(x==True):
            onState = True
            pkt = packet
            pkt.srcID = Threadnum
            pkt.pktId = packetId
            q.put(pkt)
            print(pkt.pktId, ' ', pkt.srcID)
            l = l+1
            packetId = packetId + 1
        else:
            onState = False
        time.sleep(1)
    #print(x)
    print(l/numTicks, ' threadNum', Threadnum)
    return

def PacketConsumer(q):  #dequeues and enqueues and returns the packet
    while True:
        if not q.empty():
            pkt = q.get()
            print('dequeuing Packet', ' ', pkt.pktId, pkt.srcID)
            time.sleep(1)
    return
if __name__ == '__main__':
    numTicks = 40
    q1 = Queue()
    pkt = packet
    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))
    t.start()
    x.start()
    #y.start()
    x.join()
#    y.join()
    t.join()

It gives me the following error:

Exception in thread Thread-2:
Traceback (most recent call last):
  File "C:\Users\Syed\AppData\Local\Programs\Python\Python37-32\lib\threading.py", line 917, in _bootstrap_inner
    self.run()
  File "C:\Users\Syed\AppData\Local\Programs\Python\Python37-32\lib\threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
TypeError: PacketConsumer() argument after * must be an iterable, not Queue

Upvotes: 2

Views: 138

Answers (1)

MyNameIsCaleb
MyNameIsCaleb

Reputation: 4489

A single item tuple must have a , in it to distinguish it as such, otherwise it's just grouping parenthesis.

Change this line:

t = threading.Thread(target=PacketConsumer, args=(q1,))

Upvotes: 1

Related Questions