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}")
Bedankt voor je feedback!
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
Can you explain how ThreadPoolExecutor works in this example?
What is the difference between ThreadPoolExecutor and ProcessPoolExecutor?
How can I change the number of workers in the pool?
Awesome!
Completion rate improved to 6.25
Thread Pools and Process Pools
Veeg om het menu te tonen
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}")
Bedankt voor je feedback!