from concurrent.futures import ThreadPoolExecutor from threading import Thread import time from cpuinfo.cpuinfo import get_cpu_info class ThreadPool(): def __init__(self, core=None): self.thread_count = core or get_cpu_info()["count"]*128 self.pool = ThreadPoolExecutor(max_workers = self.thread_count) self.quit = False self.started_threads = [] self.threads = {1:[],2:[],3:[],4:[],5:[]} self.starter = Thread(target=self.start_thread) self.starter.start() self.sleep=False def schedule_thread(self, priority:int, args): if priority > 5: return if len(self.threads[1]) > 100 and priority == 1: return self.threads[priority].append(args) self.sleep=False def start_thread(self): while True: try: threads = self.threads if threads[5]: thread=threads[5][0] level=5 elif threads[4]: thread=threads[4][0] level=4 elif threads[3]: thread=threads[3][0] level=3 elif threads[2]: thread=threads[2][0] level=2 elif threads[1]: thread=threads[1][0] level=1 else: self.sleep=True thread=None if len(self.started_threads) < self.thread_count and thread: try: coso=self.pool.submit(thread[0], *thread[1], **thread[2]) self.started_threads.append(coso) self.threads[level].pop(0) except IndexError: coso=self.pool.submit(thread[0], thread[1]) self.started_threads.append(coso) self.threads[level].pop(0) try: for x in range(len(self.started_threads)): if self.started_threads[x].done(): self.started_threads.pop(x) except: pass except: pass if self.sleep: time.sleep(0.01) if self.quit: return ThreadPool = ThreadPool()