|
| 1 | +""" |
| 2 | +In a multi-threaded download, this algorithm could be used to provide |
| 3 | +each worker thread with a block of non-overlapping bytes to download. |
| 4 | +For example: |
| 5 | + for i in allocation_list: |
| 6 | + requests.get(url,headers={'Range':f'bytes={i}'}) |
| 7 | +""" |
1 | 8 | from __future__ import annotations |
2 | 9 |
|
3 | 10 |
|
4 | 11 | def allocation_num(number_of_bytes: int, partitions: int) -> list[str]: |
5 | 12 | """ |
6 | 13 | Divide a number of bytes into x partitions. |
| 14 | + :param number_of_bytes: the total of bytes. |
| 15 | + :param partitions: the number of partition need to be allocated. |
| 16 | + :return: list of bytes to be assigned to each worker thread |
7 | 17 |
|
8 | | - In a multi-threaded download, this algorithm could be used to provide |
9 | | - each worker thread with a block of non-overlapping bytes to download. |
10 | | - For example: |
11 | | - for i in allocation_list: |
12 | | - requests.get(url,headers={'Range':f'bytes={i}'}) |
13 | | -
|
14 | | - parameter |
15 | | - ------------ |
16 | | - : param number_of_bytes |
17 | | - : param partitions |
18 | | -
|
19 | | - return |
20 | | - ------------ |
21 | | - : return: list of bytes to be assigned to each worker thread |
22 | | -
|
23 | | - Examples: |
24 | | - ------------ |
25 | 18 | >>> allocation_num(16647, 4) |
26 | | - ['0-4161', '4162-8322', '8323-12483', '12484-16647'] |
27 | | - >>> allocation_num(888, 888) |
28 | | - Traceback (most recent call last): |
29 | | - ... |
30 | | - ValueError: partitions can not >= number_of_bytes! |
| 19 | + ['1-4161', '4162-8322', '8323-12483', '12484-16647'] |
| 20 | + >>> allocation_num(50000, 5) |
| 21 | + ['1-10000', '10001-20000', '20001-30000', '30001-40000', '40001-50000'] |
31 | 22 | >>> allocation_num(888, 999) |
32 | 23 | Traceback (most recent call last): |
33 | 24 | ... |
34 | | - ValueError: partitions can not >= number_of_bytes! |
| 25 | + ValueError: partitions can not > number_of_bytes! |
35 | 26 | >>> allocation_num(888, -4) |
36 | 27 | Traceback (most recent call last): |
37 | 28 | ... |
38 | 29 | ValueError: partitions must be a positive number! |
39 | 30 | """ |
40 | 31 | if partitions <= 0: |
41 | 32 | raise ValueError("partitions must be a positive number!") |
42 | | - if partitions >= number_of_bytes: |
43 | | - raise ValueError("partitions can not >= number_of_bytes!") |
| 33 | + if partitions > number_of_bytes: |
| 34 | + raise ValueError("partitions can not > number_of_bytes!") |
44 | 35 | bytes_per_partition = number_of_bytes // partitions |
45 | | - allocation_list = [f"0-{bytes_per_partition}"] |
46 | | - for i in range(1, partitions - 1): |
47 | | - length = f"{bytes_per_partition * i + 1}-{bytes_per_partition * (i + 1)}" |
48 | | - allocation_list.append(length) |
49 | | - allocation_list.append( |
50 | | - f"{(bytes_per_partition * (partitions - 1)) + 1}-" f"{number_of_bytes}" |
51 | | - ) |
| 36 | + allocation_list = [] |
| 37 | + for i in range(partitions): |
| 38 | + start_bytes = i * bytes_per_partition + 1 |
| 39 | + end_bytes = ( |
| 40 | + number_of_bytes if i == partitions - 1 else (i + 1) * bytes_per_partition |
| 41 | + ) |
| 42 | + allocation_list.append(f"{start_bytes}-{end_bytes}") |
52 | 43 | return allocation_list |
53 | 44 |
|
54 | 45 |
|
|
0 commit comments