import time from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor def f(a): time.sleep(3) def print_pool_info(pool): print("count: {}".format(pool.worker_count())) print("active count: {}".format(pool.active_worker_count())) print("idle count: {}".format(pool.idle_worker_count())) print("active tasks ({}): {}".format(pool.active_task_count(), pool.active_tasks())) print("waiting tasks ({}): {}".format(pool.waiting_task_count(), pool.waiting_tasks())) if __name__ == "__main__": #pool = ThreadPoolExecutor(5) pool = ProcessPoolExecutor(5) t = .4 print("Submitting 2 jobs") pool.submit(f, 1) pool.submit(f, 2) time.sleep(t) print_pool_info(pool) time.sleep(5) print("\nSubmitting 2 jobs") pool.submit(f, 3) pool.submit(f, 4) time.sleep(t) print_pool_info(pool) time.sleep(5) print("\nSubmitting 1 job") pool.submit(f, 5) time.sleep(t) print_pool_info(pool) print("\nSubmitting 2 jobs") pool.submit(f, 6) pool.submit(f, 7) time.sleep(t) print_pool_info(pool) print("\nSubmitting 4 jobs") for i in range(8, 12): pool.submit(f, i) time.sleep(t) print_pool_info(pool) print("\nSubmitting 10 jobs") pool.map(f, range(12,22)) time.sleep(t) print_pool_info(pool) print("\nSleeping 5 seconds...") time.sleep(5) print_pool_info(pool) print("\nSleeping 5 seconds...") time.sleep(5) print_pool_info(pool)