From 9ed2927c0bc01f24f9f2b9499e33520e7a56f2c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20S=C3=A1nchez?= Date: Tue, 19 Aug 2025 19:15:47 -0300 Subject: [PATCH] 2025.08.19 - Improved thread handling --- main.py | 48 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 10 deletions(-) diff --git a/main.py b/main.py index 901e887..de20e24 100644 --- a/main.py +++ b/main.py @@ -4,6 +4,7 @@ from libraries.wrappers import earn_okx from libraries.wrappers import earn_gateio from libraries.earner import earner from libraries.colors import colors +from concurrent.futures import ThreadPoolExecutor, as_completed from threading import Thread from flask import Flask, jsonify, request from waitress import serve @@ -11,6 +12,8 @@ import time import datetime import json import sqlite3 +import signal +import os def load_keys_from_db(file_name: str) -> list: @@ -61,16 +64,23 @@ def seconds_to_time(total_seconds: float) -> str: def main(): + executor = ThreadPoolExecutor(max_workers=len(earners)) while True: threads = [] #Run earners - for item in earners: - threads.append(Thread(target=item.run)) - for item in threads: - item.start() - for item in threads: - item.join() + futures = [executor.submit(e.run) for e in earners] + for future in as_completed(futures): + try: + future.result() + except Exception as e: + print(f"Error in thread - {e}") + # for item in earners: + # threads.append(Thread(target=item.run)) + # for item in threads: + # item.start() + # for item in threads: + # item.join() #Print status subscriptions = [] @@ -485,9 +495,22 @@ def run_API(port): #earn_api.run(host="0.0.0.0", port=port) +executor = None +#Shutdown handler +def shutdown_handler(signum, _): + print(f"Received signal {signum}, shutting down as gracefully as possible...") + if executor: + executor.shutdown(wait=True, timeout=5) + os._exit(0) + +# Register signals for shutdown handler +signal.signal(signal.SIGINT, shutdown_handler) +signal.signal(signal.SIGTERM, shutdown_handler) + + if __name__=="__main__": - version = "2025.03.16" + version = "2025.08.19" start_time = time.time() with open("config.json") as f: @@ -506,11 +529,16 @@ if __name__=="__main__": valid_keys = load_keys_from_db("keys/api_credentials.db") #Threads to run: main loop and flask api - main_threads = [Thread(target=main),Thread(target=run_API, args=(config["api_port"],))] + api_thread = Thread(target=run_API, args=(config["api_port"],), daemon=True) #Iterate indefinitely: - for m in main_threads: - m.start() + api_thread.start() + + try: + main() + except KeyboardInterrupt: + api_thread.join() + shutdown_handler(signal.SIGINT, None)