Concurrent Python Programming using a ThreadPoolExecutor

preview_player
Показать описание

In this tutorial we'll be looking at how you can use the ThreadPoolExecutor in order to speed up the performance of your I/O bound Python programs.

If you found this video useful then please feel free to like the video and subscribe to the channel for more Python Programming Tutorials!
Рекомендации по теме
Комментарии
Автор

Thanks 🙏 for the nice Tutorial - very helpful!
Here is my suggestion how to improve latest code example:
```
import threading
import time
from concurrent.futures import ThreadPoolExecutor


def task(number):
print(f"Start execution by
result = 0
for i in range(number):
result += i
time.sleep(3)
print(f"result = {result}, executed by


def main():
print("Waiting threads execution...")
with as executor:
executor.submit(task, 5)
executor.submit(task, 10)
executor.submit(task, 15)
print("All treads started execution...")
print("All treads are executed successfully")


if __name__ == '__main__':
main()

```

*Benefits:*
1. All threads started simultaneously and you may see that in output
2. Each thread has unique argument and result, so you can define the order of execution
3. Each thread works 3 seconds, so you may see appropriate output delay which simulates thread execution

freeman
Автор

I loved my Concurrency lectures at Uni. I learned about C, Java and Go. Nice to know about other languages too.

tumblrbulbasaur
Автор

How can I terminate the all the threads when the python program exits: (even after ctrl+c the thread is not terminated).
Here is code sample :
with as executor:
result = executor.map(function_name, iterator)

gfdsarewq
Автор

Can this or does this have to be used in conjunction with a thread local? An example would be running webdrivers in parallel would require each thread have its own driver properties isolated from the other threads.

I'm not sure if I still have to include the thread local if using an executor or if I get that for free, and I'm not sure if I do get it free I still don't have to implement it in order to execute the custom logic in my thread local to ensure the drivers are all quit and sessions are closed when selenium is torn down

And if I do have to include a thread local, I'm not sure how if at all it plugs into the executor service or if I have to maintain it separately (by far I would say the easiest and cleanest solution would be if any task that is called in the executor service is added to the thread local and removed once that task has completed)

sean
Автор

how to give name to the submitted thread, and how to check the specific thread is running

haifa
Автор

how to make an input in max_workers?
example:
threads = input('Number of Threads: ')
with as thread:
i get error from this.

OMFGNation
Автор

Nice simple clear introduction, thanks!

nathankrowitz
Автор

At 3:01 what keys did u pressed to add 4 spaces at all lines of code?

thebrowserpiratesteam
Автор

Maybe good video but sometimes hard to understand due to accent.

ELHAUKEZ
join shbcf.ru