Python Threading Tutorial: Run Code Concurrently Using the Threading Module

Python Threading Tutorial: Run Code Concurrently Using the Threading Module

Corey Schafer

4 года назад

813,977 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Corey Schafer
Corey Schafer - 12.09.2019 17:58

Hey everybody! I hope you find this video helpful. I'll be releasing the multiprocessing video next week. I am currently packing up my temporary recording station and will be moving into my new place tomorrow, so I should be able to get it recorded, edited, and released next week for sure. We'll be covering how to use multiprocessing to process the high-resolution images we downloaded in this video. Hope you all get some good use out of these topics!

Ответить
Marios Constantinou
Marios Constantinou - 20.09.2023 15:31

This dude is like the bible for programming tutorials.

Ответить
yuval158
yuval158 - 28.08.2023 23:12

But when I tried it out, it looked like threads are switching between each other even if there is no "dead time" in the threads. Are they switching switching between each other or are they actually happening in parallel (are they actually multiprocessing)?


import threading

def cpu_bound_task(game_id):
i = 0
while i < 10**7:
i += 1
print(f"Game {game_id} CPU-bound task - iteration {i}")

def main():
threads = []

for game_id in range(3):
thread = threading.Thread(target=cpu_bound_task, args=(game_id,))
threads.append(thread)
thread.start()

for thread in threads:
thread.join()

if _name_ == "__main__":
main()

Ответить
Azema Shaik
Azema Shaik - 14.08.2023 12:38

Thank you so much, Corey, for this fantastic tutorial on introducing threading in Python to beginners! Your clear explanations and step-by-step demonstrations have really helped me grasp this concept much better. Your tutorials are always a go-to resource for me, and I appreciate how you make even complex topics so accessible. Your contribution to the Python community is invaluable, catering to both newcomers like me and experienced developers. Keep up the fantastic work, Corey!

Ответить
Julian Reichelt
Julian Reichelt - 03.08.2023 10:20

nice video!

Ответить
Anon Fourtyfive
Anon Fourtyfive - 30.07.2023 02:21

omg.
I was using some of it, but not all of it's potential.
Amazing.

Ответить
Ansel Janson
Ansel Janson - 28.07.2023 15:28

still valid even after 3 yrs
Awesome tutorial

Ответить
unknown
unknown - 25.07.2023 19:58

I think you meant asynchronously a couple of times

Ответить
Vishnu P
Vishnu P - 14.06.2023 07:37

Can we use thread in APIs(created in fastapi), does it cause database deadlock?

Ответить
Yegiii
Yegiii - 26.05.2023 09:19

why do I get None between my thread and finished in thread pool executor?

Ответить
Abdelgader Nooreldaiem
Abdelgader Nooreldaiem - 13.04.2023 23:25

thanks alot ,
and for those who the method didn't work for after using with concurrent.futures.ThreadPoolExecutor() as executor :
add max_workers=(to more than or equal to the number in the range function)
like this
with concurrent.futures.ThreadPoolExecutor(max_workers=20) as executor :

Ответить
Uday Singh
Uday Singh - 03.04.2023 04:47

So threads in python only uses one core of CPU of underlying server?

Ответить
Eduardo Siqueira
Eduardo Siqueira - 23.03.2023 11:07

Amazing video, thank you!!

Ответить
Shreyam
Shreyam - 22.03.2023 06:01

Thank you

Ответить
hotcrossbun26
hotcrossbun26 - 11.03.2023 10:42

I'd be so screwed without your videos. The BEST explanations anywhere on the internet for learning python.

Ответить
Alex Nefayne
Alex Nefayne - 06.03.2023 11:34

Your a great teacher i keep comming back to your lessons!

Ответить
Abdullah Al Nayem
Abdullah Al Nayem - 08.02.2023 17:12

Thanks

Ответить
XTheory
XTheory - 06.02.2023 18:36

when I See Someone is appreciate your video. I don't know why i'm just feel so fine💝💝

Ответить
Martín Mehaudy
Martín Mehaudy - 27.01.2023 22:48

Amazing! Tried something similar with multiprocess, but will test this one out. Thanks!

Ответить
Oh !
Oh ! - 12.01.2023 04:30

Corey I love you

Ответить
priya_explicit
priya_explicit - 11.01.2023 20:06

your'e awesome

Ответить
imanqolymoly
imanqolymoly - 09.01.2023 00:59

I am blown away

Ответить
Brater Games
Brater Games - 01.01.2023 18:29

For five years I was looking for some simple explanation that I could use in my code, to pull my dna, from snpedia...OMG. So simple but very good. Thanks man.

Ответить
Brater Games
Brater Games - 01.01.2023 16:39

In the minute 24 I subscribe and like it !!!OMG

Ответить
Pavan Tripathi
Pavan Tripathi - 29.12.2022 12:58

Thanks for the wonderful video

Ответить
Rupam Solanki
Rupam Solanki - 25.12.2022 08:46

Thanks man.

Ответить
Ibrahim Akanbi
Ibrahim Akanbi - 16.12.2022 23:39

I don't know if this comment will be seen but I'm having trouble running the concurrent.futures method with a for loop

Ответить
Reem Aljabari
Reem Aljabari - 09.12.2022 21:33

Thank youu so much Highlyy recommended , you just express it in a simple way

Ответить
Arya iye
Arya iye - 03.12.2022 20:14

Beautiful !

Ответить
Time Passer
Time Passer - 01.12.2022 13:14

can I perform multithreading from dictionary? i cant seem to find out the syntax :(

Ответить
New Pain
New Pain - 30.11.2022 07:53

Completely agree about the basic example tutorials, I often find them useless. Great video, thanks!

Ответить
Sonu Singh
Sonu Singh - 27.11.2022 07:44

What would be candidate for doing something with json, like extracting and storing, multi threading?

Ответить
stelloprint
stelloprint - 18.11.2022 08:52

I am curious about a best practice for dividing up a list of REST API calls so that chunks can be sent to different processes via multi-processing first, then each process can use multi-threading to request data and subsequently process the payloads individually. Basically a good way of using both techniques in such a way that you leverage the strengths of multi-threading within a multi-process operation which I know can be done.

There's just a bit of magic that thread/process pools are doing where they determine how to allocate workers for you and I don't know how to use that magic to divide the list of REST calls optimally per process. I guess I could count the number of CPUs available on the machine but then I kind of like how process pools does that for you and helps make your operations safe without potentially maxing out all processors.

Ответить
O1 Tech Academy
O1 Tech Academy - 06.11.2022 17:35

Thank you for this powerful tutorial.

Ответить
Anant Chopra
Anant Chopra - 26.10.2022 22:05

This video is a life-saver! I have a project due in 3 days that needs me to do a lot of fancy stuff in a multithreaded program, and I had no clue what multithreaded programs are! I'm in a much better position after watching this video. Thank you, Corey!

Ответить
Ethan
Ethan - 24.10.2022 09:35

what a godsend tutorial

Ответить
Paul Cal
Paul Cal - 06.10.2022 17:07

Thanks Corey. Why do you pass in the args as a list [1.5]?

Ответить
Tony Wong
Tony Wong - 26.09.2022 12:48

Thank you for this. The Thread Pool Executor is really simple to use.

Ответить
Aditya Kappagantula
Aditya Kappagantula - 20.09.2022 15:31

Thank for the detailed step by step explanation. How do we control the number of threads using a ThreadPoolExecutor when processing a list of say 40k urls? For example if I want to process 10urls at a time. Is creating a smaller sublist with 10 items from the main 40k list an ideal solution?

Ответить
BrainWithNoPain
BrainWithNoPain - 27.08.2022 11:22

this is kinda interesting that you can run a code in parallel withing for loop THANKS COREY

Ответить
Luffing Sails
Luffing Sails - 22.08.2022 19:46

Tried Skillshare to take on learning Python. There is a bunch of milquetoast videos with no real substance. These videos by Corey are outstanding. Much appreciation. I'm glad it was a free subscription.

Ответить
SkySalt
SkySalt - 22.08.2022 16:54

Hey Corey, thanks for this. You are an awesome teacher. I will be watching your other videos too as needed and Subscribed! :D

Ответить
26_dharmesh marathe
26_dharmesh marathe - 19.08.2022 21:17

You're great. Your video on multithreading helped me to undertand the differncce between multithread & multiprocess. Great explaination!!❤❤

Ответить