# ****************************************************** # 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()) # ******************************************************
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)
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.
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.
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()
>>> 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
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??
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?
Hello, you probably wrote parentheses when declaring - target= for example: x = threading.Thread(target=eat_breakfast(), args=()) Remove this and let's check again :)
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.
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
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()
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)
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?
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)
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?
# ******************************************************
# 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())
# ******************************************************
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)
thank you I understood that , but I am really wondering why did not happen to him
wow man thank you very much, i was looking at my code for like 10 minutes and couldnt figure it out
came here just for this. thank you
@@KudoShinichii1412 Same, does anyone know why Bro didn't have to subtract two perf counters? Is there maybe some setting for this?
Thx! I think without your solution that function counted seconds from the point of my pc was turned on.
Really like how you go to the heart of the subject.. Concise and clear... Thanks..
I really like how you explain everything so simply and quickly. Keep it up man !
I second that!
This is brilliant .
Excellent explanation !!!
👏👏👏
Thank you for these clear and precise explanations.
As I am new to Python, this becomes very practical for my learning.
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.
Such a wonderful explanation..
nice video bro
dobrze rozkminione :) Dziekowa
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.
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()
i was wondering why mine was different. thx!
@@baldwin9207but why, now it works
Understood in one go. Good work bro
great explanation, loved the theory before the actual code
love u
Super, finally i learn this argument! :) Nice work!!!
hope the algorithm blesses your channel
THAT IS ACTUALLY REALLY COOL NGL
Excellent Explanation !!!
Thanks for this
>>> 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
great
Great video thanks
great explanation, thanks
you sir are the best
brooo you da bestt!!
TY bro
so simply, thankss
thakns
thx 4 vid br
o!
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.
شكرا جزيلا
Even though you sound like 'Butthead' from 'Beavis & Butthead', I still love you 'Bro'. And thanks for your awesome tutorials. 👍🏻👍🏻
amazing thank!s
Wow!!!!!
Nice.
Print("Amazing")
Bro.. 👏 Heads down.
Thank you very much
nice
breh.... this is so clear...
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??
Crystal Clear!
thank youuu
oh yea its cool
Bro, Do they only work with functions?
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?
Hello, you probably wrote parentheses when declaring - target=
for example:
x = threading.Thread(target=eat_breakfast(), args=())
Remove this and let's check again :)
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.
@@blizni3371 jesus Christ, thanks man, i was in a hole for like 2 hourse before figuring out this!
@@MrPsichoKid No problem!
Thanks a lot my friend ! Iwas trying to figure out why my example was faulty @@blizni3371
Thank you!
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
meow~! uwu
Make a tutorial for flutter please beer is on me 🍺
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()
nevermind. figured it out.
My main tread is taking 734078.1110504 seconds to complete its task. what possibly could be the issue?
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)
does not work 4 me?? how to unlock gil in pycharm
yeah.. multi threading in the morning.. sounds familiar. like brushing teeth while getting the pants on.. =)
ate
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?
Yup, me too
just divide it with 10,000 and you will get 3.4 secs
somewhy i have 374690.9 seconds
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)
Kindly revealed your face , we want to sees a person who know every language exist in this world
should i be concerned that when i run the same code as you i get "590447.4203372" returned from "print(time.perf_counter())"
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
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?
I got the same issue :\
Me too
Are you taking the difference between values? Because the zero point is implementation-defined.
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)