2020-10-05 03:47:46 +00:00
|
|
|
"""
|
|
|
|
In a multi-threaded download, this algorithm could be used to provide
|
|
|
|
each worker thread with a block of non-overlapping bytes to download.
|
|
|
|
For example:
|
|
|
|
for i in allocation_list:
|
|
|
|
requests.get(url,headers={'Range':f'bytes={i}'})
|
|
|
|
"""
|
2024-03-13 06:52:41 +00:00
|
|
|
|
2020-09-23 11:30:13 +00:00
|
|
|
from __future__ import annotations
|
2020-03-20 14:24:05 +00:00
|
|
|
|
|
|
|
|
2020-09-23 11:30:13 +00:00
|
|
|
def allocation_num(number_of_bytes: int, partitions: int) -> list[str]:
|
2020-03-20 14:24:05 +00:00
|
|
|
"""
|
|
|
|
Divide a number of bytes into x partitions.
|
2020-10-05 03:47:46 +00:00
|
|
|
:param number_of_bytes: the total of bytes.
|
|
|
|
:param partitions: the number of partition need to be allocated.
|
|
|
|
:return: list of bytes to be assigned to each worker thread
|
2020-05-22 06:10:11 +00:00
|
|
|
|
2020-03-20 14:24:05 +00:00
|
|
|
>>> allocation_num(16647, 4)
|
2020-10-05 03:47:46 +00:00
|
|
|
['1-4161', '4162-8322', '8323-12483', '12484-16647']
|
|
|
|
>>> allocation_num(50000, 5)
|
|
|
|
['1-10000', '10001-20000', '20001-30000', '30001-40000', '40001-50000']
|
2020-03-20 14:24:05 +00:00
|
|
|
>>> allocation_num(888, 999)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
2020-10-05 03:47:46 +00:00
|
|
|
ValueError: partitions can not > number_of_bytes!
|
2020-03-20 14:24:05 +00:00
|
|
|
>>> allocation_num(888, -4)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
ValueError: partitions must be a positive number!
|
|
|
|
"""
|
|
|
|
if partitions <= 0:
|
2020-03-28 06:24:59 +00:00
|
|
|
raise ValueError("partitions must be a positive number!")
|
2020-10-05 03:47:46 +00:00
|
|
|
if partitions > number_of_bytes:
|
|
|
|
raise ValueError("partitions can not > number_of_bytes!")
|
2020-03-20 14:24:05 +00:00
|
|
|
bytes_per_partition = number_of_bytes // partitions
|
2020-10-05 03:47:46 +00:00
|
|
|
allocation_list = []
|
|
|
|
for i in range(partitions):
|
|
|
|
start_bytes = i * bytes_per_partition + 1
|
|
|
|
end_bytes = (
|
|
|
|
number_of_bytes if i == partitions - 1 else (i + 1) * bytes_per_partition
|
|
|
|
)
|
|
|
|
allocation_list.append(f"{start_bytes}-{end_bytes}")
|
2020-03-20 14:24:05 +00:00
|
|
|
return allocation_list
|
|
|
|
|
|
|
|
|
2020-03-28 06:24:59 +00:00
|
|
|
if __name__ == "__main__":
|
2020-03-20 14:24:05 +00:00
|
|
|
import doctest
|
2020-03-28 06:24:59 +00:00
|
|
|
|
2020-03-20 14:24:05 +00:00
|
|
|
doctest.testmod()
|