import multiprocessing
import queue
import time
import os
def worker(q, task_id):
"""Worker function to process tasks from the queue."""
while True:
try:
task = q.get(timeout=1) # Get task, timeout after 1 second
if task is None:
break # Exit if None is received
print(f"Worker {task_id}: Processing task {task}")
time.sleep(0.5) # Simulate task processing
print(f"Worker {task_id}: Finished task {task}")
q.task_done() # Signal task completion
except queue.Empty:
# Queue is empty, check for more tasks
pass
def create_queue_based_script(num_workers, num_tasks):
"""
Creates a queue-based script for multiprocessing.
Args:
num_workers: Number of worker processes.
num_tasks: Total number of tasks to be processed.
"""
task_queue = queue.Queue()
processes = []
# Create and start worker processes
for i in range(num_workers):
p = multiprocessing.Process(target=worker, args=(task_queue, i))
processes.append(p)
p.start()
# Add tasks to the queue
for i in range(num_tasks):
task_queue.put(i)
# Block until all tasks are done
task_queue.join()
# Signal workers to exit
for _ in range(num_workers):
task_queue.put(None)
# Wait for all worker processes to finish
for p in processes:
p.join()
if __name__ == '__main__':
num_workers = 4
num_tasks = 20
create_queue_based_script(num_workers, num_tasks)
print("All tasks completed.")
Add your comment