1

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.

simplycoding
  • 2,770
  • 9
  • 46
  • 91
  • 3
    (not *exactly* the same question, but most of the answers to that question involve splitting a range into chunks, which is exactly what you want) – Samwise Apr 22 '21 at 21:26
  • why do you want 1-5 and 5-10 ??? you only have 9 pages so the second group will for sure have 1 less – DevLounge Apr 22 '21 at 21:28
  • @Samwise that's what I figured I'd have to do. I tried it but was similarly stumped on getting the last chunk to be the right size if it was 1 size too big – simplycoding Apr 22 '21 at 21:42
  • @DevLounge because I'm iterating over it as range(start, end) – simplycoding Apr 22 '21 at 21:42

0 Answers0