Threads and multiprocessing
A thread is a line of execution: several threads can move forward at the same time in one program. In Python they're great for tasks that wait (downloads, files, network). But because of the GIL (Global Interpreter Lock), only one thread runs Python code at a time: they don't speed up heavy calculations.
For heavy calculations, you use multiprocessing: several processes, each with its own interpreter, which really run in parallel on several cores. The concurrent.futures module makes both easy with ThreadPoolExecutor and ProcessPoolExecutor. Note: the browser can't start threads here, so the example shows the sequential version and the code to try on your computer.
def download(name):
return f"{name} downloaded"
files = ["a.pdf", "b.pdf", "c.pdf"]
results = list(map(download, files))
print(results)
# On your computer, the same work with 3 threads at once:
# from concurrent.futures import ThreadPoolExecutor
# with ThreadPoolExecutor(max_workers=3) as pool:
# results = list(pool.map(download, files))