In [22]:
import numpy as np
import threading
from queue import Queue
import time
import random

In [23]:
class thread_object:
    def __init__(self, job_id, signal):
        """ Create instance of a thread object, helpes sorting data
        
        :type  job_id: int
        :param job_id: the job ID assigned from threading
        
        :type  signal: ndarray
        :param signal: the signal-array from code
        """
        self.job_id = job_id
        self.signal = signal
    def __repr__(self):
        return "Job ID: {}\n {}\n\n".format(self.job_id, self.signal)

In [24]:
common_list = []

In [25]:
def job(worker):
    time.sleep(random.randint(1,10)/10)
    with write_lock:
        common_list.append(thread_object(worker, np.random.randint(0,9,(1,4))))

In [26]:
def threader():
    while True:
        worker = q.get()
        job(worker)
        q.task_done()

In [27]:
num_threads = 4
num_workers = 5

In [28]:
write_lock = threading.Lock()

q = Queue()

In [29]:
for thread_index in range(num_threads):
    thread = threading.Thread(target = threader)
    thread.daemon = True
    thread.start()

In [30]:
for worker in range(num_workers):
    q.put(worker) #check if worker can be tuple (initial,final)
q.join()

In [31]:
common_list


Out[31]:
[Job ID: 1
  [[5 6 8 7]]
 , Job ID: 2
  [[2 7 8 7]]
 , Job ID: 3
  [[7 0 8 2]]
 , Job ID: 4
  [[2 7 7 1]]
 , Job ID: 0
  [[4 6 4 5]]
 ]

In [32]:
sorted(common_list, key=lambda thread_object: thread_object.job_id)


Out[32]:
[Job ID: 0
  [[4 6 4 5]]
 , Job ID: 1
  [[5 6 8 7]]
 , Job ID: 2
  [[2 7 8 7]]
 , Job ID: 3
  [[7 0 8 2]]
 , Job ID: 4
  [[2 7 7 1]]
 ]

In [ ]: