Created
January 5, 2022 04:01
-
-
Save bannarisoftwares/b836cb860d963f5dc74f333c986402d1 to your computer and use it in GitHub Desktop.
Python handling thread without using concurrents
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 uuid | |
active_threads_ids = [] | |
no_of_threads = 4 | |
progress_task = [5, 3, 2, 1, 4, 1, 5, 5, 16, 8, 4, 8, 3] | |
progress_task_index = 0 | |
def thread_on_finish(thread_id): | |
global progress_task_index | |
active_threads_ids.remove(thread_id) | |
progress_task_index = progress_task_index + 1 | |
if len(active_threads_ids) <= no_of_threads and progress_task_index < len(progress_task): | |
start_new_thread() | |
else: | |
print("||||||all task done in this allotment||||||") | |
if len(active_threads_ids) == 0: | |
resume_remaining_work_done() | |
def thread_run(thread_id, current_progress_task_index): | |
print("|starting | {0} | {1} | {2}".format(str(current_progress_task_index), | |
str(progress_task[current_progress_task_index]), | |
str(thread_id))) | |
time.sleep(progress_task[current_progress_task_index]) | |
print("|Finishing | {0} | {1} | {2}".format(str(current_progress_task_index), | |
str(progress_task[current_progress_task_index]), | |
str(thread_id))) | |
thread_on_finish(thread_id) | |
def start_threads(no_of_thr): | |
i = 0 | |
while i < no_of_thr: | |
global progress_task_index | |
start_new_thread() | |
i = i + 1 | |
progress_task_index = progress_task_index + 1 | |
def start_new_thread(): | |
t_id = str(uuid.uuid4()) | |
active_threads_ids.append(t_id) | |
t = threading.Thread(target=thread_run, args=(t_id, progress_task_index,)) | |
t.start() | |
if __name__ == '__main__': | |
print("|state | index | value | thread_id") | |
start_threads(no_of_threads) | |
def resume_remaining_work_done(): | |
print("Resuming all tasks") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment