Skip to content

Instantly share code, notes, and snippets.

@bannarisoftwares
Created January 5, 2022 04:01
Show Gist options
  • Select an option

  • Save bannarisoftwares/b836cb860d963f5dc74f333c986402d1 to your computer and use it in GitHub Desktop.

Select an option

Save bannarisoftwares/b836cb860d963f5dc74f333c986402d1 to your computer and use it in GitHub Desktop.

Revisions

  1. bannarisoftwares created this gist Jan 5, 2022.
    57 changes: 57 additions & 0 deletions main.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,57 @@
    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")