Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from concurrent.futures import ThreadPoolExecutor
- def test(paramlist, startIndex, stopIndex):
- sum = 0
- for i in range(startIndex, stopIndex):
- sum += paramlist[i]
- return sum
- def splitproblem(total, workers):
- add = total // workers;
- remain = total % workers;
- curIndex = 0
- ret = []
- for i in range(0, workers):
- ret.append(curIndex)
- curIndex += add
- if(remain > 0):
- remain -= 1
- curIndex += 1;
- ret.append(total)
- return ret
- if __name__ == "__main__":
- testlist = range(0, 10000)
- num_threads = 12
- list_size = len(testlist)
- indices = splitproblem(list_size, num_threads)
- with ThreadPoolExecutor(max_workers=num_threads) as executor:
- futures = []
- for idx in range(0, num_threads):
- futures.append(executor.submit(test, testlist, indices[idx], indices[idx+1]))
- sum = 0
- for future in futures:
- result = future.result()
- sum += result
- print(sum)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement