我使用排队程序说明如下:
这里,我的代码:
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获取一个文件,在执行了its操作之后,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不必等待线程1的完成?因为Thread2可以从队列中获取另一项,并使用选定的文件处理应用程序的执行。你知道吗
目前没有回答
相关问题 更多 >
编程相关推荐