0

I have a some threads that are downloading content for various websites using Python's built-in urllib module. The code looks something like this:

from urllib.request import Request, urlopen
from threading import Thread

##do stuff

def download(url):
    req = urlopen(Request(url))
    return req.read()

url = "somerandomwebsite"
Thread(target=download, args=(url,)).start()
Thread(target=download, args=(url,)).start()

#Do more stuff

The user should have an option to stop loading data, and while I can use flags/events to prevent using the data when it is done downloading if the user stops the download, I can't actually stop the download itself.

Is there a way to either stop the download (and preferably do something when the download is stopped) or forcibly (and safely) kill the thread the download is running in?

Thanks in advance.

Andereoo
  • 834
  • 1
  • 8
  • 24
  • 1
    Not an exact answer to you question, but this should help: https://stackoverflow.com/questions/323972/is-there-any-way-to-kill-a-thread – James May 21 '21 at 21:34

1 Answers1

0

you can use urllib.request.urlretrieve instead witch takes a reporthook argument:

from urllib.request import urlretrieve
from threading import Thread

url = "someurl"
flag = 0

def dl_progress(count, blksize, filesize):
    global flag
    if flag:
        raise Exception('downlaod canceled')

Thread(target=urlretrieve, args=(url, "test.rar",dl_progress)).start()

if cancel_download():
    flag = 1
sina ney
  • 1
  • 1
  • Your answer works perfectly, but unfortunately, I don't think it will work for me. Since I'm downloading dozens of files and using them, saving them to the disc and then reading them is a lot of overhead. I don't really want to be saving these files either. I think I'm kind of stuck with urlopen. Thanks anyway. – Andereoo May 22 '21 at 12:56