使用动态数组python多线程

时间:2016-07-29 10:12:29

标签: python

在python中,我有一个线程来向数组添加元素。我有另一个线程,使用该数组的第一个元素然后删除它。 问题是第二个线程比第一个线程快,所以我需要等待第一个线程添加其他元素,然后处理而不是进入错误索引超出范围。

最快的方法是什么?

1 个答案:

答案 0 :(得分:3)

您应该使用Synchronised Queue类或类似的。

Queue类使用阻塞和可选超时来处理超长和欠长。它也是线程安全的。

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()

运行时:

>>> 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