Menu Close

How to use threads in Python 3 [the easy way]

techwetrust post image

In this tutorial, you’ll learn to use threading in Python 3 using the default library concurrent.futures.

What are the advantages of using concurrent.futures?

  • less code to write
  • asynchronous threads
  • a high-level interface for using threads

Basic example

from concurrent import futures

with futures.ThreadPoolExecutor() as executor:
    # submit a task to our pool of threads
    future = executor.submit(pow, 3,2)

future.result() # will print 9

Advanced example

We can use threads to process multiple tasks asynchronously. A common example is with multiple requests.

import requests
from concurrent import futures

urls = [

def make_request(url):
    response = requests.get(url)

with futures.ThreadPoolExecutor() as executor:, urls)

# This will output the URLs in a random order
# In my case of execution the output was:

The map method accepts a function name and an iterable.

function: It is a function to which map passes each element of given iterable.
iterable: Lists, tuples, dictionaries, and sets which will be mapped.

[1] Python 3 concurrent.futures

Spread the love

Leave a Reply