Last active
December 15, 2022 05:08
-
-
Save filevich/0fc34c949f1d418db75b8206e7fe15e1 to your computer and use it in GitHub Desktop.
split buffer or range in subranges for a given number of threads
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import threading | |
import time | |
import random | |
def thread_function(name, n): | |
print(f"Thread {name} starting - duration: {n}") | |
time.sleep(n) | |
if __name__ == "__main__": | |
threads = list() | |
t = 4 | |
print(f"Main crating {t} threads.") | |
for index in range(3): | |
n = random.randint(2,15) | |
x = threading.Thread(target=thread_function, args=(index,n,)) | |
threads.append(x) | |
x.start() | |
print("\n") | |
for index, thread in enumerate(threads): | |
print(f"Main: before joining thread {index}.") | |
thread.join() | |
print(f"Main: thread {index} done") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def split(buff_size, num_threads): | |
xs = [] | |
sub_buff_size = buff_size // num_threads # floor | |
for i in range(num_threads): | |
start = i * sub_buff_size | |
is_last = i+1 == num_threads | |
end = buff_size if is_last else (i + 1) * sub_buff_size | |
xs += [(start, end)] | |
return xs | |
# minified version | |
_split = lambda n, t: [(i*(n//t), n if i+1==t else (i+1)*(n//t)) for i in range (t)] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment