假设有一些任务要完成。为了完成这项任务,将使用几个过程。所以,将保持两个队列。一个包含任务,另一个包含已完成任务的日志。
然后实例化流程来完成任务。请注意,python队列类已经同步。
这意味着,我们不需要使用锁类来阻塞多个进程来访问同一个队列对象。这就是为什么,在这种情况下不需要使用锁类。
下面是将任务添加到队列中的实现,然后创建进程并启动它们,然后使用join()完成这些进程。最后,我们将从第二个队列打印日志。
from multiprocessing import Process, Queue, current_process
import time
import queue # imported for using queue.Empty exception def do_job(tasks_to_accomplish, tasks_that_are_done):
while True:
try:
'''
try to get task from the queue. get_nowait() function will
raise queue.Empty exception if the queue is empty.
queue(False) function would do the same task also.
'''
task = tasks_to_accomplish.get_nowait()
except queue.Empty: break
else:
'''
if no exception has been raised, add the task completion
message to task_that_are_done queue
'''
print(task)
tasks_that_are_done.put(task + ' is done by ' + current_process().name)
time.sleep(.5)
return True def main():
number_of_task = 10
number_of_processes = 4
tasks_to_accomplish = Queue()
tasks_that_are_done = Queue()
processes = [] for i in range(number_of_task):
tasks_to_accomplish.put("Task no " + str(i)) # creating processes
for w in range(number_of_processes):
p = Process(target=do_job, args=(tasks_to_accomplish, tasks_that_are_done))
processes.append(p)
p.start() # completing process
for p in processes:
p.join() # print the output
while not tasks_that_are_done.empty():
print(tasks_that_are_done.get()) return True if __name__ == '__main__':
main()