Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda 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

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 4. Capítulo 1

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Suggested prompts:

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?

bookThread Pools and Process Pools

Deslize para mostrar o menu

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

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 4. Capítulo 1
some-alt