Python multithreading 🧵

preview_player
Показать описание
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 its time waiting for internal events (CPU intensive)
# use multiprocessing

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

import threading
import time

def eat_breakfast():
print("You eat breakfast")

def drink_coffee():
print("You drank coffee")

def study():
print("You finish studying")

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

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

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

Рекомендации по теме
Комментарии
Автор

#
# 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.enumerate())
print(time.perf_counter())

#

BroCodez
Автор

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)

blizni
Автор

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

AmmarAlIessa
Автор

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

djn
Автор

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

thepragmatic
Автор

This is brilliant .
Excellent explanation !!!

👏👏👏

vrsionf
Автор

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.

waynefrannedavis
Автор

great explanation, loved the theory before the actual code

samuellopez
Автор

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.

lawrencedoliveiro
Автор

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()

SleepyAizawa
Автор

hope the algorithm blesses your channel

ilordepic
Автор

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

giaxlab
Автор

>>> 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

ugurbayrak
Автор

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.

FabioRBelotto