0

In python, I have a thread to add elements to an array. And I have another thread that uses the first element of that array then deletes it. The problem is that the second thread is faster than the first one, so I need that it waits for the first one to add other elements and then process instead of going into an error index is out of range.

What is the fastest way?

1 Answer 1

3

You should be using the Synchronised Queue class or similar.

The Queue class handles overlength and underlength with blocking and optional timeouts. It's also threadsafe.

import threading
import Queue
import time
import logging

logging.basicConfig(level=logging.DEBUG,format='%(threadName)s: %(message)s')

q = Queue.Queue(10)

class Producer(threading.Thread):
  def __init__(self,group=None,target=None,name=None,args=None,kwargs=None):
    if args is None:
      args = ()
    if kwargs is None:
      kwargs = {}
    super(Producer,self).__init__(group=group,target=target,name=name,args=args,kwargs=kwargs)
    self.max_count = 10
    self.delay = 3
  def run(self):
    count = 0
    logging.debug('Starting run')
    while count <= self.max_count:
      q.put(count)
      logging.debug('Putting idx {0} in queue, queue length = {1}'.format(count,q.qsize()))
      count += 1
      time.sleep(self.delay)
    logging.debug('Finished run')

class Consumer(threading.Thread):
  def __init__(self,group=None,target=None,name=None,args=None,kwargs=None):
    if args is None:
      args = ()
    if kwargs is None:
      kwargs = {}
    super(Consumer,self).__init__(group=group,target=target,name=name,args=args,kwargs=kwargs)
    self.timeout = 10
    self.delay = 1
  def run(self):
    logging.debug('Starting run')
    while True:
      try:
        work = q.get(True,self.timeout)
      except Queue.Empty:
        logging.debug('Queue still empty after {0} giving up'.format(self.timeout))
        break
      logging.debug('Received idx {0} from queue, queue length = {1}'.format(work,q.qsize()))
      time.sleep(self.delay)
    logging.debug('Finished run')

def main():
  p = Producer(name='producer')
  c = Consumer(name='consumer')
  p.daemon = True
  c.daemon = True
  p.start()
  time.sleep(8)
  c.start()

When run:

>>> main()
producer: Starting run
producer: Putting idx 0 in queue, queue length = 1
producer: Putting idx 1 in queue, queue length = 2
producer: Putting idx 2 in queue, queue length = 3
consumer: Starting run
consumer: Received idx 0 from queue, queue length = 2
producer: Putting idx 3 in queue, queue length = 3
consumer: Received idx 1 from queue, queue length = 2
consumer: Received idx 2 from queue, queue length = 1
consumer: Received idx 3 from queue, queue length = 0
producer: Putting idx 4 in queue, queue length = 1
consumer: Received idx 4 from queue, queue length = 0
producer: Putting idx 5 in queue, queue length = 1
consumer: Received idx 5 from queue, queue length = 0
producer: Putting idx 6 in queue, queue length = 1
consumer: Received idx 6 from queue, queue length = 0
producer: Putting idx 7 in queue, queue length = 1
consumer: Received idx 7 from queue, queue length = 0
producer: Putting idx 8 in queue, queue length = 1
consumer: Received idx 8 from queue, queue length = 0
producer: Putting idx 9 in queue, queue length = 1
consumer: Received idx 9 from queue, queue length = 0
producer: Putting idx 10 in queue, queue length = 1
consumer: Received idx 10 from queue, queue length = 0
producer: Finished run
consumer: Queue still empty after 10 giving up
consumer: Finished run
Sign up to request clarification or add additional context in comments.

3 Comments

An example would help.
@JulienFadel: Is this example useful?
I simply used the following syntax in my application: import queue --> queue = queue.Queue() --> queue.put('a') --> queue.get() Thats all and It did what I wanted fast.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.