Python Threading Tutorial: Run Code Concurrently Using the Threading Module
Why Stack Overflow recommends it
Stack Overflow users link it as a go-to beginner tutorial on Python threading and ThreadPoolExecutor when answering questions about speeding up loops, scraping, API calls and GUI apps.
Recommended by 11 people across 12 different months, 2020 to 2023.
What it is
A 36-minute Python tutorial by Corey Schafer on running code concurrently with the threading module. It covers creating and joining threads, then uses ThreadPoolExecutor from concurrent.futures to download multiple high-resolution images.
What people said
Recommends EL TEGANI MOHAMED · 2020-01 · Stack Overflow · on “Multiprocessing vs Threading Python”
Threads share the same memory space to guarantee that two threads don't share the same memory location so special precautions must be taken the CPython interpreter handles this using a mechanism called GIL , or the Global Interpreter Lock what is GIL (Just I want to Clarify GIL it's repeated above)? In CPython, the global interpreter lock, or GIL, is a mutex that protects access to Python objects, preventing multiple threads from executing Python bytecodes at once. This lock is necessary mainly because CPython's memory management is not thread-safe. For the main question, we can compare using Use Cases, How? 1-Use Cases for Threading : in case of GUI programs threading can be used to make th
Recommends Jakub Szlaur · 2020-02 · Stack Overflow · on “Separating Tkinters GUI and control of the application”
1) What is my goal: I’m creating an application that should read data every 60s from ModBusServer, append those data to Graphs and then when the app is closed save the data to excel file. Site note: The process of reading data from ModBusServer and appending them to graphs should start after a start button is pressed. And end after stop button is pressed OR when ModBusServer sends a request to stop. 2) What I have so far: I created the GUI without any major problems as a class “GUI_komora”. Everything there works just fine. 3) What is the problem: But now I’m lost on how to approach the “read data every 60 seconds”, and overall how to control the application. I did some research on threading
Recommends Omid Ki · 2020-04 · Stack Overflow · on “How can I speed up this "for" loop?”
As martineau said, you can use threading to speed up your program. Just import the "threading" library and add threding.Thread in your class's parentheses and name your function "def run(self):" then put whatever you like your thread to do inside that function and wherever you want to start your thread use your object name and then ".start()" this way it will start your thread. Most of the times it is not a good idea to do this but if you have to then you should do it. By the way if your program is more I/O (input and output) bound then it is better to use threads, if it is CPU bound it should be better for you to use multi-processing. You can also watch these tutorials for better understand
Recommends Sideeg MoHammed · 2020-05 · Stack Overflow · on “Running two functions in parallel and for the same time”
you must learn what thread is i prepare this one to learn in this tutrial [this video] but for your question and if you know what thread is what mention here [LINK] can help you
Recommends Matiiss · 2020-10 · Stack Overflow · on “Running non Tkinter code along side Tkinter code”
i wont be able to tell but here is a tutorial for threading [this video] with real life example and tutorial for multiprocessing [LINK] with real life example.
Recommends Matiiss · 2021-03 · Stack Overflow · on “Is Playsound able to play two sounds at the same time?”
well as far as I understand it means that You run another thread in paralle or more like concurrently but then look at multiprocessing, look here [this video]
Recommends Matiiss · 2021-04 · Stack Overflow · on “Am I correct thinking that multiprocessing is better for my trading bot than threads?”
I don't know whether multiprocessing is an advantege, check these two tutorials where I think it is also explained for what they are used: threading [this video] , multiprocessing [LINK]
Recommends Matiiss · 2021-04 · Stack Overflow · on “Creating "Stop" button using tkinter & classes”
@SultryT. I think You will have to use threads if You don't know anything about them I suggest this tutorial [this video] , since already it was explained that loops can interfere with the `.mainloop()` so at least some issues were solved but You will have to use threads I think
Recommends Maximilian Freitag · 2021-10 · Stack Overflow · on “Solving System of Nonlinear Equations in 100 variables Computationally”
You can use threading for your purposes. Here an explanation from Corey Schäfer --> [this video] The core idea is to run code in paralellel, meaning your code doesn't go like "...compute first code block ... (finished).... compute second code block ... finished" and so on. With threading your code will run multiple code blocks at once. A good python library is the multiprocessing library. But first make sure how many processors you can utilize for your machine. import multiprocessing as mp print("Number of processors: ", mp.cpu_count()) --> Number of processors: 4 For example my PC only has 4 processors available And here an example of threading within python: import requests from multiprocessing
Recommends Ali Hejazizo · 2021-12 · Stack Overflow · on “Python - Speeding up Web Scraping using multiprocessing”
Why don't you use concurrent.futures module? Here is a very simple but super fast code using concurrent.futures : import concurrent.futures def scrape_url(url): print(f'Scraping {url}...') scraped_content = '<html>scraped content!</html>' return scraped_content urls = [' [LINK] ', ' [LINK] ', ' [LINK] '] with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor: results = executor.map(scrape_url, urls) print(list(results)) # Expected output: # ['<html>scraped content!</html>', '<html>scraped content!</html>', '<html>scraped content!</html>'] If you want to learn threading, I recommend watching this short tutorial: [this video] Also note that this is not multiprocessing, this is multit