Тёмный

Python multithreading 🧵 

Bro Code
Подписаться 1,9 млн
Просмотров 62 тыс.
50% 1

python threading multithreading tutorial example explained
#python #threading #multithreading
******************************************************
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())
******************************************************

Наука

Опубликовано:

 

3 июл 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 86   
@BroCodez
@BroCodez 3 года назад
# ****************************************************** # 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()) # ******************************************************
@notwma7274
@notwma7274 3 года назад
Best tutorial i have ever seen!
@blizni3371
@blizni3371 2 года назад
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)
@KudoShinichii1412
@KudoShinichii1412 10 месяцев назад
thank you I understood that , but I am really wondering why did not happen to him
@Mildic
@Mildic 9 месяцев назад
wow man thank you very much, i was looking at my code for like 10 minutes and couldnt figure it out
@NoPo1nt
@NoPo1nt 9 месяцев назад
came here just for this. thank you
@lukaskelber
@lukaskelber 8 месяцев назад
@@KudoShinichii1412 Same, does anyone know why Bro didn't have to subtract two perf counters? Is there maybe some setting for this?
@Dmitopur
@Dmitopur 7 месяцев назад
Thx! I think without your solution that function counted seconds from the point of my pc was turned on.
@AmmarAlIessa
@AmmarAlIessa 2 года назад
Really like how you go to the heart of the subject.. Concise and clear... Thanks..
@waynefrannedavis9305
@waynefrannedavis9305 4 месяца назад
Interesting to see how Python addresses multi-processing and synchronization when compared to Elixir which is my go to language. At 75 years old, I am finally looking at object-orientation.
@entity5678
@entity5678 Год назад
Such a wonderful explanation..
@user-vr2si8on9f
@user-vr2si8on9f 10 месяцев назад
This is brilliant . Excellent explanation !!! 👏👏👏
@thepragmatic6383
@thepragmatic6383 Год назад
Thank you for these clear and precise explanations. As I am new to Python, this becomes very practical for my learning.
@aliforootani9348
@aliforootani9348 2 года назад
great explanation, thanks
@samuellopez5155
@samuellopez5155 2 года назад
great explanation, loved the theory before the actual code
@piotrkopcewicz5227
@piotrkopcewicz5227 Год назад
dobrze rozkminione :) Dziekowa
@d1jn
@d1jn 2 года назад
I really like how you explain everything so simply and quickly. Keep it up man !
@kyokokirigiri166
@kyokokirigiri166 9 месяцев назад
I second that!
@ilhamm1915
@ilhamm1915 Год назад
THAT IS ACTUALLY REALLY COOL NGL
@giaxfaidate
@giaxfaidate 2 года назад
Super, finally i learn this argument! :) Nice work!!!
@felixnyamongo
@felixnyamongo Год назад
Understood in one go. Good work bro
@VenkataChalaBhaskar
@VenkataChalaBhaskar Год назад
Excellent Explanation !!!
@Johann.Liebert
@Johann.Liebert Год назад
so simply, thankss
@nirutg5130
@nirutg5130 3 года назад
Thank you very much
@dolevdo
@dolevdo 2 года назад
amazing thank!s
@gamerawesome8105
@gamerawesome8105 2 года назад
you sir are the best
@ilordepic
@ilordepic Год назад
hope the algorithm blesses your channel
@shuaibalghazali3405
@shuaibalghazali3405 Год назад
Thanks for this
@bekturasanbekov1979
@bekturasanbekov1979 11 месяцев назад
thx 4 vid br o!
@brianwake100
@brianwake100 2 года назад
Great video thanks
@lawrencedoliveiro9104
@lawrencedoliveiro9104 2 года назад
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.
@infinitecrafterYT
@infinitecrafterYT 10 месяцев назад
nice video bro
@Amir_Plays_non_stop
@Amir_Plays_non_stop 3 года назад
Crystal Clear!
@lw9954
@lw9954 Год назад
TY bro
@kamlesht.j9366
@kamlesht.j9366 Год назад
brooo you da bestt!!
@ugurbayrak9869
@ugurbayrak9869 2 года назад
>>> 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
@ahiamatagabriel5696
@ahiamatagabriel5696 2 года назад
thank youuu
@SuperStarEevee
@SuperStarEevee Год назад
Thank you!
@Daniel-cl6hj
@Daniel-cl6hj 3 года назад
breh.... this is so clear...
@EissaAlahdul
@EissaAlahdul Год назад
شكرا جزيلا
@beingzero7541
@beingzero7541 2 года назад
Wow!!!!!
@philtoa334
@philtoa334 3 года назад
Nice.
@HussainAli-sb1dv
@HussainAli-sb1dv 7 месяцев назад
love u
@SleepyAizawa69
@SleepyAizawa69 Год назад
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()
@baldwin9207
@baldwin9207 Год назад
i was wondering why mine was different. thx!
@autoauto2000
@autoauto2000 5 месяцев назад
@@baldwin9207but why, now it works
@airexpertdrop
@airexpertdrop 3 года назад
oh yea its cool
@FabioRBelotto
@FabioRBelotto Год назад
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.
@charan2446
@charan2446 2 года назад
Bro.. 👏 Heads down.
@uuhju7004
@uuhju7004 2 года назад
nice
@aaroncatolico7550
@aaroncatolico7550 Год назад
Even though you sound like 'Butthead' from 'Beavis & Butthead', I still love you 'Bro'. And thanks for your awesome tutorials. 👍🏻👍🏻
@rubenc4696
@rubenc4696 Месяц назад
thakns
@gustavoaponte1814
@gustavoaponte1814 Год назад
meow~! uwu
@Pjnpm
@Pjnpm 2 года назад
Make a tutorial for flutter please beer is on me 🍺
@xcorpionxyed2078
@xcorpionxyed2078 2 года назад
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??
@RenovaLabs
@RenovaLabs 2 месяца назад
Bro, Do they only work with functions?
@locrilydian
@locrilydian 2 года назад
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?
@blizni3371
@blizni3371 2 года назад
Hello, you probably wrote parentheses when declaring - target= for example: x = threading.Thread(target=eat_breakfast(), args=()) Remove this and let's check again :)
@stevemiller123
@stevemiller123 Год назад
I have the exact same issue, did you resolve it? Even when copying and pasting the code from the description. It's behaving as if I joined all the threads even when I haven't. So when I actually write, x.join(), y.join(), z,join().. the behaver is the same.
@MrPsichoKid
@MrPsichoKid Год назад
@@blizni3371 jesus Christ, thanks man, i was in a hole for like 2 hourse before figuring out this!
@blizni3371
@blizni3371 Год назад
@@MrPsichoKid No problem!
@ligdjumvidja8294
@ligdjumvidja8294 6 месяцев назад
Thanks a lot my friend ! Iwas trying to figure out why my example was faulty @@blizni3371
@moisesherrera71
@moisesherrera71 3 месяца назад
but what if a function returns a value, how should we write that so we have the function in a separate thread then main but we can capture the return value of the function
@cinquecento1985
@cinquecento1985 Год назад
yeah.. multi threading in the morning.. sounds familiar. like brushing teeth while getting the pants on.. =)
@manuelvaal1257
@manuelvaal1257 2 года назад
My main tread is taking 734078.1110504 seconds to complete its task. what possibly could be the issue?
@hinter9907
@hinter9907 2 года назад
the solution: # add this line just before running x.start() start_time = time.perf_counter() # add these lines after z.join end_time = time.perf_counter() delta_time = end_time - start_time print(delta_time)
@ClydeKilgore-df8hy
@ClydeKilgore-df8hy 3 месяца назад
Why do I get the following error after importing threading and attempting to use it? AttributeError: partially initialized module 'threading' has no attribute 'Thread' (most likely due to a circular import) same message when I use active_count()
@ClydeKilgore-df8hy
@ClydeKilgore-df8hy 3 месяца назад
nevermind. figured it out.
@estudio7753
@estudio7753 2 года назад
ate
@autoauto2000
@autoauto2000 5 месяцев назад
does not work 4 me?? how to unlock gil in pycharm
@aiviral_
@aiviral_ 2 года назад
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?
@wilsonsoeparman8574
@wilsonsoeparman8574 2 года назад
Yup, me too
@mittlope2723
@mittlope2723 2 года назад
just divide it with 10,000 and you will get 3.4 secs
@mm-fn9uj
@mm-fn9uj 2 года назад
somewhy i have 374690.9 seconds
@hinter9907
@hinter9907 2 года назад
the solution: # add this line just before running x.start() start_time = time.perf_counter() # add these lines after z.join end_time = time.perf_counter() delta_time = end_time - start_time print(delta_time)
@techboomers8935
@techboomers8935 Год назад
Kindly revealed your face , we want to sees a person who know every language exist in this world
@Sstevewong36
@Sstevewong36 4 месяца назад
great
@reinkdesigns
@reinkdesigns Год назад
should i be concerned that when i run the same code as you i get "590447.4203372" returned from "print(time.perf_counter())"
@ramanuj_g
@ramanuj_g Год назад
check above comments as the answer is there above.... nothing to be concerned just u have to add start time and subtract it from end time
@ixmjk
@ixmjk 3 года назад
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?
@aiviral_
@aiviral_ 2 года назад
I got the same issue :\
@wilsonsoeparman8574
@wilsonsoeparman8574 2 года назад
Me too
@lawrencedoliveiro9104
@lawrencedoliveiro9104 2 года назад
Are you taking the difference between values? Because the zero point is implementation-defined.
@nakiros24
@nakiros24 2 года назад
this worked for me: 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") begin_time = time.perf_counter() 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()-begin_time)
Далее
threading vs multiprocessing in python
22:31
Просмотров 563 тыс.
Python multiprocessing ⚡
9:31
Просмотров 27 тыс.
FARUX RAIMOV AVJIGA CHIQDI - JAVOHIR🔥
01:01
Просмотров 634 тыс.
СПРАВКА ДЛЯ УНИВЕРА
00:44
Просмотров 348 тыс.
DO NOT Dunk Here ❌🏀
00:20
Просмотров 8 млн
Python if __name__ == '__main__' ❓
5:41
Просмотров 34 тыс.
Multithreading Code - Computerphile
15:54
Просмотров 380 тыс.
Unlocking your CPU cores in Python (multiprocessing)
12:16
CONCURRENCY IS NOT WHAT YOU THINK
16:59
Просмотров 88 тыс.
A first look at a faster, no-GIL Python
6:56
Просмотров 7 тыс.
Python Threading Explained in 8 Minutes
8:39
Просмотров 129 тыс.
YOTAPHONE 2 - СПУСТЯ 10 ЛЕТ
15:13
Просмотров 156 тыс.