Introduction to multithreading in Python

A Python application runs on a single thread, unless you explicitly enable multithreading.

Why is multithreading useful? Code in Python is ran in sequence, one instruction after another.

If you define a function that sleeps 3 seconds and then prints something, like this:

import time

def greet():
    time.sleep(3)
    print('hello')

greet()
print('world')

The world string is printed after 3 seconds, because we call time.sleep(3) inside the greet() function.

This is just a silly example, but imagine processing an image, getting a resource from the network, or writing a big file to disk. Anything that can take a lot of time.

With multithreading we can run the function that takes a lot of time into a separate thread, and go on with our program in the meantime.

The threading standard library module helps with implementing multithreading. You import Thread from it:

from threading import Thread

Then we pass the function we must execute as the target argument to the Thread() function, getting a thread object:

t = Thread(target=greet)

then we call its start() method to start the thread:

t.start()

Try running this code:

from threading import Thread
import time

def greet():
    time.sleep(3)
    print('hello')

t = Thread(target=greet)
t.start()

print('world')

You will now see world printed 3 seconds before hello shows up in the console.

The program does not end until the thread (or all the threads it started) end, unless you start a thread as a deamon.

Those are the basics of multithreading. This is complex, and can lead to the introduction of bugs if not done well.


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com

A Python application runs on a single thread, unless you explicitly enable multithreading.

Why is multithreading useful? Code in Python is ran in sequence, one instruction after another.

If you define a function that sleeps 3 seconds and then prints something, like this:

import time

def greet():
    time.sleep(3)
    print('hello')

greet()
print('world')

The world string is printed after 3 seconds, because we call time.sleep(3) inside the greet() function.

This is just a silly example, but imagine processing an image, getting a resource from the network, or writing a big file to disk. Anything that can take a lot of time.

With multithreading we can run the function that takes a lot of time into a separate thread, and go on with our program in the meantime.

The threading standard library module helps with implementing multithreading. You import Thread from it:

from threading import Thread

Then we pass the function we must execute as the target argument to the Thread() function, getting a thread object:

t = Thread(target=greet)

then we call its start() method to start the thread:

t.start()

Try running this code:

from threading import Thread
import time

def greet():
    time.sleep(3)
    print('hello')

t = Thread(target=greet)
t.start()

print('world')

You will now see world printed 3 seconds before hello shows up in the console.

The program does not end until the thread (or all the threads it started) end, unless you start a thread as a deamon.

Those are the basics of multithreading. This is complex, and can lead to the introduction of bugs if not done well.


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com


Print Share Comment Cite Upload Translate Updates
APA

flaviocopes.com | Sciencx (2021-02-25T05:00:00+00:00) Introduction to multithreading in Python. Retrieved from https://www.scien.cx/2021/02/25/introduction-to-multithreading-in-python/

MLA
" » Introduction to multithreading in Python." flaviocopes.com | Sciencx - Thursday February 25, 2021, https://www.scien.cx/2021/02/25/introduction-to-multithreading-in-python/
HARVARD
flaviocopes.com | Sciencx Thursday February 25, 2021 » Introduction to multithreading in Python., viewed ,<https://www.scien.cx/2021/02/25/introduction-to-multithreading-in-python/>
VANCOUVER
flaviocopes.com | Sciencx - » Introduction to multithreading in Python. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/02/25/introduction-to-multithreading-in-python/
CHICAGO
" » Introduction to multithreading in Python." flaviocopes.com | Sciencx - Accessed . https://www.scien.cx/2021/02/25/introduction-to-multithreading-in-python/
IEEE
" » Introduction to multithreading in Python." flaviocopes.com | Sciencx [Online]. Available: https://www.scien.cx/2021/02/25/introduction-to-multithreading-in-python/. [Accessed: ]
rf:citation
» Introduction to multithreading in Python | flaviocopes.com | Sciencx | https://www.scien.cx/2021/02/25/introduction-to-multithreading-in-python/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.