Thread Pools and Process Pools
When you need to run many tasks concurrently, creating and managing each thread or process manually quickly becomes cumbersome and error-prone. This is where the concept of pools comes in. A pool is a collection of worker threads or processes that are managed for you, allowing you to submit tasks without worrying about the low-level details of starting, stopping, or reusing workers. By using pools, you can efficiently distribute work, limit resource usage, and write cleaner, more maintainable code. Python provides the concurrent.futures module, which includes ThreadPoolExecutor for threads and ProcessPoolExecutor for processes. These executors make it easy to submit tasks and collect results asynchronously.
1234567891011121314151617import concurrent.futures import time def square(n): time.sleep(1) return n * n numbers = [1, 2, 3, 4, 5] results = [] with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor: future_to_num = {executor.submit(square, num): num for num in numbers} for future in concurrent.futures.as_completed(future_to_num): num = future_to_num[future] result = future.result() results.append((num, result)) print(f"Square of {num} is {result}")
Kiitos palautteestasi!
Kysy tekoälyä
Kysy tekoälyä
Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme
Awesome!
Completion rate improved to 6.25
Thread Pools and Process Pools
Pyyhkäise näyttääksesi valikon
When you need to run many tasks concurrently, creating and managing each thread or process manually quickly becomes cumbersome and error-prone. This is where the concept of pools comes in. A pool is a collection of worker threads or processes that are managed for you, allowing you to submit tasks without worrying about the low-level details of starting, stopping, or reusing workers. By using pools, you can efficiently distribute work, limit resource usage, and write cleaner, more maintainable code. Python provides the concurrent.futures module, which includes ThreadPoolExecutor for threads and ProcessPoolExecutor for processes. These executors make it easy to submit tasks and collect results asynchronously.
1234567891011121314151617import concurrent.futures import time def square(n): time.sleep(1) return n * n numbers = [1, 2, 3, 4, 5] results = [] with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor: future_to_num = {executor.submit(square, num): num for num in numbers} for future in concurrent.futures.as_completed(future_to_num): num = future_to_num[future] result = future.result() results.append((num, result)) print(f"Square of {num} is {result}")
Kiitos palautteestasi!