How can I use threading in Python?

前端 未结 19 2663
迷失自我
迷失自我 2020-11-21 04:54

I am trying to understand threading in Python. I\'ve looked at the documentation and examples, but quite frankly, many examples are overly sophisticated and I\'m having trou

19条回答
  •  孤街浪徒
    2020-11-21 05:26

    Most documentation and tutorials use Python's Threading and Queue module, and they could seem overwhelming for beginners.

    Perhaps consider the concurrent.futures.ThreadPoolExecutor module of Python 3.

    Combined with with clause and list comprehension it could be a real charm.

    from concurrent.futures import ThreadPoolExecutor, as_completed
    
    def get_url(url):
        # Your actual program here. Using threading.Lock() if necessary
        return ""
    
    # List of URLs to fetch
    urls = ["url1", "url2"]
    
    with ThreadPoolExecutor(max_workers = 5) as executor:
    
        # Create threads
        futures = {executor.submit(get_url, url) for url in urls}
    
        # as_completed() gives you the threads once finished
        for f in as_completed(futures):
            # Get the results
            rs = f.result()
    

提交回复
热议问题