Python multithreading

Python multithreading

Bro Code

3 года назад

60,878 Просмотров

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


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

Bro Code
Bro Code - 08.02.2021 19:21

# ****************************************************
# Python threading tutorial
# ****************************************************
# thread = a flow of execution. Like a separate order of instructions.
# However each thread takes a turn running to achieve concurrency
# GIL = (global interpreter lock),
# allows only one thread to hold the control of the Python interpreter at any one time

# cpu bound = program/task spends most of it's time waiting for internal events (CPU intensive)
# use multiprocessing

# io bound = program/task spends most of it's time waiting for external events (user input, web scraping)
# use multithreading

import threading
import time


def eat_breakfast():
time.sleep(3)
print("You eat breakfast")


def drink_coffee():
time.sleep(4)
print("You drank coffee")


def study():
time.sleep(5)
print("You finish studying")


x = threading.Thread(target=eat_breakfast, args=())
x.start()

y = threading.Thread(target=drink_coffee, args=())
y.start()

z = threading.Thread(target=study, args=())
z.start()

x.join()
y.join()
z.join()

print(threading.active_count())
print(threading.enumerate())
print(time.perf_counter())

# ****************************************************

Ответить
infinitecrafter
infinitecrafter - 03.09.2023 14:44

nice video bro

Ответить
ilyass babkhouti
ilyass babkhouti - 14.08.2023 21:07

This is brilliant .
Excellent explanation !!!

👏👏👏

Ответить
Bektur Asanbekov
Bektur Asanbekov - 22.07.2023 13:16

thx 4 vid br
o!

Ответить
SuperGuavo
SuperGuavo - 06.06.2023 21:31

meow~! uwu

Ответить
Ilham M
Ilham M - 15.05.2023 14:41

THAT IS ACTUALLY REALLY COOL NGL

Ответить
SleepyAizawa
SleepyAizawa - 01.02.2023 07:02

Firstly thank you bro
Secondly if you guys have problem with main thread not printing 4then you must delete the() for writting the function in x=threading.thread()

Ответить
shuaib alghazali
shuaib alghazali - 16.01.2023 09:04

Thanks for this

Ответить
Kamlesh T.J
Kamlesh T.J - 10.01.2023 17:46

brooo you da bestt!!

Ответить
Sommer81
Sommer81 - 26.12.2022 14:12

so simply, thankss

Ответить
The Pragmatic
The Pragmatic - 20.12.2022 02:59

Thank you for these clear and precise explanations.
As I am new to Python, this becomes very practical for my learning.

Ответить
Fábio Belotto
Fábio Belotto - 18.12.2022 22:40

I would like that you showed an example like this : you can eat and drink at same time, but you must finish such activities to study.

Ответить
Piotr Kopcewicz
Piotr Kopcewicz - 05.12.2022 23:07

dobrze rozkminione :) Dziekowa

Ответить
Bhaskar GUNDU
Bhaskar GUNDU - 19.11.2022 06:15

Excellent Explanation !!!

Ответить
Tech Boomers
Tech Boomers - 21.10.2022 21:38

Kindly revealed your face , we want to sees a person who know every language exist in this world

Ответить
Matthias Burger
Matthias Burger - 19.10.2022 03:05

yeah.. multi threading in the morning.. sounds familiar. like brushing teeth while getting the pants on.. =)

Ответить
Aaron Catolico
Aaron Catolico - 02.09.2022 18:09

Even though you sound like 'Butthead' from 'Beavis & Butthead', I still love you 'Bro'. And thanks for your awesome tutorials. 👍🏻👍🏻

Ответить
Felix Ondieki Nyamongo
Felix Ondieki Nyamongo - 02.09.2022 09:32

Understood in one go. Good work bro

Ответить
LW99
LW99 - 31.08.2022 13:32

TY bro

Ответить
Random dude
Random dude - 28.08.2022 18:40

hope the algorithm blesses your channel

Ответить
أبو مصعب
أبو مصعب - 22.08.2022 11:58

شكرا جزيلا

Ответить
XxPINGAS_LAUNCHERxX
XxPINGAS_LAUNCHERxX - 16.08.2022 05:53

Thank you!

Ответить
Alankrith
Alankrith - 25.07.2022 22:19

Such a wonderful explanation..

Ответить
ReinkDesigns
ReinkDesigns - 22.07.2022 06:12

should i be concerned that when i run the same code as you i get "590447.4203372" returned from "print(time.perf_counter())"

Ответить
Ahiamata Gabriel
Ahiamata Gabriel - 21.06.2022 02:01

thank youuu

Ответить
Xcorpion Xyed
Xcorpion Xyed - 13.06.2022 15:22

Hey bro I've a problem.
Whenever I write my own code (following the same procedure) it shows only 1 thread and takes allotted time, but when I copy the given description code it and paste it, shows the 4 threads
Can't figure out why is it happening??

Ответить
Bliźni
Bliźni - 03.06.2022 17:58

If you have problem with huge amount of time displaying by time.perf_counter() function here you have solve of this problem:
We can read in documentation:
time.perf_counter()
[...] The reference point of the returned value is undefined, so that only the difference between the results of two calls is valid.

So to solve it we have to declare variable before our code, for example:

starting_point = time.perf_counter()
...
our code here
...
print (time.perf_counter() - starting_point)

Ответить
Giax fai da te
Giax fai da te - 25.04.2022 00:04

Super, finally i learn this argument! :) Nice work!!!

Ответить
Estudio
Estudio - 14.04.2022 15:23

ate

Ответить
Samuel Lopez
Samuel Lopez - 21.03.2022 20:46

great explanation, loved the theory before the actual code

Ответить
Ammar Al-Iessa
Ammar Al-Iessa - 18.03.2022 15:40

Really like how you go to the heart of the subject.. Concise and clear... Thanks..

Ответить
Brian Sheehan
Brian Sheehan - 27.02.2022 07:31

Great video thanks

Ответить
Being ZERO
Being ZERO - 04.02.2022 20:02

Wow!!!!!

Ответить
Ali Dogac Kose
Ali Dogac Kose - 30.01.2022 17:57

for some reason my main thread waits for the other 3 threads to finish before it executes the print functions. i wrote the exact same code he wrote. anyone have an idea?

Ответить
manuel vaal
manuel vaal - 13.01.2022 03:30

My main tread is taking 734078.1110504 seconds to complete its task. what possibly could be the issue?

Ответить
E. E.
E. E. - 06.01.2022 19:19

I really like how you explain everything so simply and quickly. Keep it up man !

Ответить
Lawrence D’Oliveiro
Lawrence D’Oliveiro - 06.01.2022 09:30

For I/O bound problems, it is better to use asyncio than threads. This gives less opportunity for race conditions and their consequent hard-to-reproduce bugs.

Ответить
HanotzTv
HanotzTv - 02.01.2022 19:21

amazing thank!s

Ответить
ali forootani
ali forootani - 25.11.2021 11:40

great explanation, thanks

Ответить
Ugur Bayrak
Ugur Bayrak - 19.11.2021 14:06

>>> Very consistent explanation :)
>>> Could you please do this kind of tuts regarding python standart libr modules like Struct, OS, SubProcess and Select ?
>>> Have a good time

Ответить
Charan
Charan - 06.11.2021 18:04

Bro.. 👏 Heads down.

Ответить
Inspired Life
Inspired Life - 21.10.2021 20:50

Hey I got this problem with Python showing 34568.4580 seconds while it only takes 3-4 seconds and it's not the only case in which this happens. Does anyone know how to display seconds correcty?

Ответить
Gamer Awesome 810
Gamer Awesome 810 - 22.09.2021 01:53

you sir are the best

Ответить
Pat M
Pat M - 06.08.2021 15:19

Make a tutorial for flutter please beer is on me 🍺

Ответить
uuhju
uuhju - 22.07.2021 09:21

nice

Ответить
Amirreza Akbari
Amirreza Akbari - 14.07.2021 14:18

Thank you Bro
i run the exact same code but for me the time.perf_counter() returns a really big value for time taken sth like 11929.1326382 but in reality it takes 5 to 6 seconds to run i search online for solutions but nothing came out.
Any solutions?

Ответить
GaMe tOuT
GaMe tOuT - 19.06.2021 19:45

Crystal Clear!

Ответить
NotWma
NotWma - 06.06.2021 21:42

Best tutorial i have ever seen!

Ответить
NirutG
NirutG - 28.05.2021 12:13

Thank you very much

Ответить
Daniel
Daniel - 04.05.2021 02:31

breh.... this is so clear...

Ответить