+ 2
How is Multithreading achived in Python ?
A thread acquires GIL, does a little work, then passes the GIL onto the next thread.
2 odpowiedzi
+ 4
import threading
def function():
while True:
print(”threading”)
th = threading.Thread(target=function)
th.start()
+ 3
First import the threading module, define functions representing the tasks you want the threads to perform. Create thread instances and specify the target
Use start() to start a thread, you can as well wait for a thread to finish using the join().
But you can save yourself the trouble by using other approaches like multiprocessing or asynchronous programming due to the fact that in Python because of the GIL, python threads are more suitable for I/O bound tasks than CPU-bound tasks.