2025.08.19 - Improved thread handling

This commit is contained in:
Nicolás Sánchez 2025-08-19 19:15:47 -03:00
parent 86ed6830f4
commit 9ed2927c0b
1 changed files with 38 additions and 10 deletions

48
main.py
View File

@ -4,6 +4,7 @@ from libraries.wrappers import earn_okx
from libraries.wrappers import earn_gateio from libraries.wrappers import earn_gateio
from libraries.earner import earner from libraries.earner import earner
from libraries.colors import colors from libraries.colors import colors
from concurrent.futures import ThreadPoolExecutor, as_completed
from threading import Thread from threading import Thread
from flask import Flask, jsonify, request from flask import Flask, jsonify, request
from waitress import serve from waitress import serve
@ -11,6 +12,8 @@ import time
import datetime import datetime
import json import json
import sqlite3 import sqlite3
import signal
import os
def load_keys_from_db(file_name: str) -> list: def load_keys_from_db(file_name: str) -> list:
@ -61,16 +64,23 @@ def seconds_to_time(total_seconds: float) -> str:
def main(): def main():
executor = ThreadPoolExecutor(max_workers=len(earners))
while True: while True:
threads = [] threads = []
#Run earners #Run earners
for item in earners: futures = [executor.submit(e.run) for e in earners]
threads.append(Thread(target=item.run)) for future in as_completed(futures):
for item in threads: try:
item.start() future.result()
for item in threads: except Exception as e:
item.join() 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 #Print status
subscriptions = [] subscriptions = []
@ -485,9 +495,22 @@ def run_API(port):
#earn_api.run(host="0.0.0.0", port=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__": if __name__=="__main__":
version = "2025.03.16" version = "2025.08.19"
start_time = time.time() start_time = time.time()
with open("config.json") as f: with open("config.json") as f:
@ -506,11 +529,16 @@ if __name__=="__main__":
valid_keys = load_keys_from_db("keys/api_credentials.db") valid_keys = load_keys_from_db("keys/api_credentials.db")
#Threads to run: main loop and flask api #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: #Iterate indefinitely:
for m in main_threads: api_thread.start()
m.start()
try:
main()
except KeyboardInterrupt:
api_thread.join()
shutdown_handler(signal.SIGINT, None)