Add CPU affinity management and garbage collection to optimize performance
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import time
|
||||
import gc
|
||||
import asyncio
|
||||
import requests
|
||||
from utils.logger import setup_logging
|
||||
@@ -10,8 +10,10 @@ from utils.clob_client_and_order import (
|
||||
place_anchor_and_hedge,
|
||||
cache_tocken_trading_infos,
|
||||
)
|
||||
from utils.cpu_affinity import set_cpu_affinity
|
||||
|
||||
|
||||
gc.disable()
|
||||
session = requests.Session()
|
||||
requests.get = session.get
|
||||
requests.post = session.post
|
||||
@@ -27,6 +29,7 @@ async def main():
|
||||
trades = 0
|
||||
|
||||
logger = setup_logging()
|
||||
set_cpu_affinity()
|
||||
logger.info("Polymarket HFT Market Maker started")
|
||||
|
||||
up_token, down_token, market_slug = await fetch_tokens()
|
||||
@@ -54,6 +57,7 @@ async def main():
|
||||
|
||||
book.stop()
|
||||
logger.info("Trading session ended. Starting new session.")
|
||||
gc.collect()
|
||||
await asyncio.sleep(10)
|
||||
up_token, down_token, market_slug = await fetch_tokens()
|
||||
book = OrderBook(up_token, down_token, market_slug)
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
import os
|
||||
import psutil
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def set_cpu_affinity():
|
||||
try:
|
||||
process = psutil.Process()
|
||||
cpu_count = os.cpu_count()
|
||||
logger.info(f"System has {cpu_count} CPU cores")
|
||||
if cpu_count >= 4:
|
||||
affinity_cores = [cpu_count - 2, cpu_count - 1]
|
||||
elif cpu_count >= 2:
|
||||
affinity_cores = [cpu_count - 1]
|
||||
else:
|
||||
affinity_cores = [0]
|
||||
|
||||
process.cpu_affinity(affinity_cores)
|
||||
logger.info(f"CPU affinity set to cores: {affinity_cores}")
|
||||
|
||||
if os.name == "nt": # Windows
|
||||
process.nice(psutil.HIGH_PRIORITY_CLASS)
|
||||
logger.info("Process priority set to HIGH")
|
||||
else: # Linux/Unix
|
||||
process.nice(-10) # Higher priority (lower nice value)
|
||||
logger.info("Process nice value set to -10 (high priority)")
|
||||
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to set CPU affinity: {e}")
|
||||
Reference in New Issue
Block a user