Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Thread Pools and Process Pools | Advanced Patterns and Best Practices
Python Multithreading and Multiprocessing

bookThread 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.

1234567891011121314151617
import 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}")
copy
question mark

Which of the following is an advantage of using a thread or process pool instead of manually creating threads or processes for each task?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 4. Capítulo 1

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

bookThread Pools and Process Pools

Desliza para mostrar el menú

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.

1234567891011121314151617
import 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}")
copy
question mark

Which of the following is an advantage of using a thread or process pool instead of manually creating threads or processes for each task?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 4. Capítulo 1
some-alt