并发执行线程

时间:2017-05-21 09:22:22

标签: python multithreading python-2.7

我使用队列实现了生产者/消费者模型。程序描述如下:

  • producer从某个文件夹中获取4个文件,并将每个文件的绝对路径放入队列
  • 消费者(在我的情况下:4)从队列中取出文件路径并做某事。用它(例如:将它交给应用程序执行)

在这里,我的代码:

queue_condition = threading.Condition()
shared_queue = Queue(maxsize=4)

def consumer(condition):
            with condition:
                 # wait as long the queue is empty
                 while shared_queue.empty():
                       logger.info("[%s] - waiting for elements in queue ..." % threading.current_thread().name)
                       condition.wait()
                 else:
                       # take the selected path from the queue
                       item = shared_queue.get()
                       logger.info("[%s] - has selected the file %s" % (threading.current_thread().name, item))

                       # copy it to another place
                       start_application(item)


                       shared_queue.task_done()
                       logger.debug("[%s] has finished its task" % threading.current_thread().name)

#############################################################################

def producerCM(condition):
            logging.debug('Starting selection ...')

            with condition:

                 # as long as queue is not full
                 # start picking a file
                 while not shared_queue.full():
                       # select the file
                       filename = pick_file()
                       selectedFilePath = basepath + "\\" + filename

                       item = selectedFilePath
                       logging.debug("Selected File: " + selectedFilePath)
                      shared_queue.put(item)

                      logging.debug("Notifying consumer threads that the queue is ready to consume ..")
                      condition.notifyAll()



threads = [threading.Thread(target=consumer, args=(queue_condition,)) for i in range(4)]

for thread in threads:
                thread.setDaemon(True)

[thread.start() for thread in threads]

prod = threading.Thread(name='producer_task_thread', target=producerCM, args=(queue_condition,))
prod.setDaemon(True)
prod.start()

[thread.join() for thread in threads]

我的问题是,例如,Thread1接受一个文件,在其操作之后,Thread2开始从队列中获取另一个文件。 但我想要的是他们应该同时开始。那么,为什么Thread2需要等待Thread1?

这里输出:

2017-05-21 13:48:35,053 - [Thread-1] - waiting for elements in queue ...
2017-05-21 13:48:35,055 - [Thread-2] - waiting for elements in queue ...
2017-05-21 13:48:35,055 - [Thread-3] - waiting for elements in queue ...
2017-05-21 13:48:35,055 - [Thread-4] - waiting for elements in queue ...
2017-05-21 13:48:35,056 - Starting selection...
2017-05-21 13:48:35,056 - Selected File: C:\Users\k1rk1r\Desktop\AvIo\samples\aecore.dll
2017-05-21 13:48:35,058 - Selected File: C:\Users\k1rk1r\Desktop\AvIo\samples\aecrypto.dll
2017-05-21 13:48:35,058 - Selected File: C:\Users\k1rk1r\Desktop\AvIo\samples\aebb.dll
2017-05-21 13:48:35,059 - Selected File: C:\Users\k1rk1r\Desktop\AvIo\samples\aecore.dll
2017-05-21 13:48:35,059 - Notifying fibonacci_task threads that the queue is ready to consume ..
2017-05-21 13:48:35,059 - [Thread-1] - has selected the file C:\Users\k1rk1r\Desktop\AvIo\samples\aecore.dll
Command is: C:\Program Files\FRISK Software\F-PROT Antivirus for Windows\fpscan.exe -r -d 100 -z 99 C:\Users\k1rk1r\Desktop\AvIo\samples\aecore.dll
2017-05-21 13:48:35,059 - [Thread-2] - has selected the file C:\Users\k1rk1r\Desktop\AvIo\samples\aebnd.dll
Command is: C:\Program Files\FRISK Software\F-PROT Antivirus for Windows\fpscan.exe -r -d 100 -z 99 C:\Users\k1rk1r\Desktop\AvIo\samples\aebnd.dll

例如:如何告诉线程2没有必要等待Thread1的完成?因为Thread2可以从队列中获取另一个项目并使用所选文件处理应用程序的执行。

0 个答案:

没有答案