Search code examples
pythonmultithreadingblockingpyglet

How to do a non-blocking URL fetch in Python


I am writing a GUI app in Pyglet that has to display tens to hundreds of thumbnails from the Internet. Right now, I am using urllib.urlretrieve to grab them, but this blocks each time until they are finished, and only grabs one at a time.

I would prefer to download them in parallel and have each one display as soon as it's finished, without blocking the GUI at any point. What is the best way to do this?

I don't know much about threads, but it looks like the threading module might help? Or perhaps there is some easy way I've overlooked.


Solution

  • You'll probably benefit from threading or multiprocessing modules. You don't actually need to create all those Thread-based classes by yourself, there is a simpler method using Pool.map:

    from multiprocessing import Pool
    
    def fetch_url(url):
        # Fetch the URL contents and save it anywhere you need and
        # return something meaningful (like filename or error code),
        # if you wish.
        ...
    
    pool = Pool(processes=4)
    result = pool.map(f, image_url_list)