I want to make a multi-threaded downloader (in Python) and I need to tell each thread where to start and how many bytes to download. For that I get the remote file size and divide it, for example, to 2. Now, let’s say that the remote file size is 5: when I divide the number to 2, I get 2 as result. Now I can start the download but I will lose a byte (because 2*2=4, not 5). I can’t use float numbers because I can’t download half of a byte. How I could divide that number and to get a list with [2, 3], for example?
I want to make a multi-threaded downloader (in Python) and I need to tell
Share
Use
divmod:This tells you, that 5 divided by 2 is 2, remainder 1, so the last piece will be 2 + 1 = 3.
Here, you’ll have 5 chunks at 2057 and a last slice at 2057+3.
This algorithm will also work for cases, where division is without remainder:
Here, you’ll have 4 chunks at 2469 plus a last slice at 2469+0.
So, your chunk sizes could be computed as: