I'm trying to setup threads with an even number of API pages to query for each thread. Example below has 9 pages that I want to call in total, and I want to split into 2 threads: first thread would ideally query pages 1-4, and the second thread would query pages 5-9. But I'm having some trouble getting it to evenly and properly distribute. The following works for threads = 2
and an even number of pages, but not for odd number of pages because I call the variables as range(page_start, page_end)
and want the 9th page:
import math
pages = 9
page_end = 1
threads = 2
for thread in range(1,1 + threads):
page_start = page_end
page_end = page_start + math.floor(pages/threads)
print(page_start, page_end)
# Output is
# 1 5
# 5 9
# when it should be the following
# 1 5
# 5 10
Every time I fix this, it breaks when pages
is set to an even number.