Search code examples
pythonmultithreadingconcurrent.futures

Python concurrent.futures: threads don't start


I made a program that starts 4 threads using concurrent.futures and sends a request to each website in a list.

Problem: It just doesn't start

import requests
import threading
import concurrent.futures

list=['amazon.com', 'google.com', 'youtube.com', 'microsoft.com']

def start(url):
    requests.get('https://'+url)
    print(line+' finished')

with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
    for url in list:
        executor.submit(start, url)

This on the other hand works:

import concurrent.futures

def start():
    print('lol')

with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
    for i in range(10):
        executor.submit(start)

Solution

  • You've just scheduled the tasks but you don't wait for the results and you have an error in your code (use of undefined variable) however the error message was "swallowed" by the context manager.

    The executor.submit method returns a Future object where you can get the return value by calling result method:

    import requests
    import threading
    import concurrent.futures
    
    URLS = ['amazon.com', 'google.com', 'youtube.com', 'microsoft.com']
    
    
    def start(url):
        requests.get('https://' + url)
        print(f'{url}: finished')
    
    
    def main():
        futures = []
    
        with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
            for url in URLS:
                futures.append(executor.submit(start, url))
    
            for future in futures:
                future.result()
    
    
    if __name__ == "__main__":
        main()