From 4a07cb7f180aa8e110a501740f866d894c94e8b7 Mon Sep 17 00:00:00 2001 From: Ichinga Samuel Date: Sun, 1 Dec 2024 00:15:45 +0100 Subject: [PATCH] v4.0.5 --- examples/sample_bot.py | 14 ++++++++++++-- src/aiomql/lib/executor.py | 18 ++++++++++++++++-- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/examples/sample_bot.py b/examples/sample_bot.py index 74a6fe1..627168f 100644 --- a/examples/sample_bot.py +++ b/examples/sample_bot.py @@ -1,7 +1,8 @@ import logging +import asyncio from aiomql.lib.bot import Bot -from aiomql.contrib.strategies import FingerTrap +from aiomql.contrib.strategies import FingerTrap, Chaos from aiomql.contrib.symbols import ForexSymbol @@ -9,10 +10,19 @@ def sample_bot(): logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s") syms = ["Volatility 75 Index", "Volatility 100 Index", "Volatility 50 Index"] symbols = [ForexSymbol(name=sym) for sym in syms] - strategies = [FingerTrap(symbol=symbol) for symbol in symbols] + strategies = [Chaos(symbol=symbol) for symbol in symbols] bot = Bot() + bot.executor.timeout = 10 + + bot.add_coroutine(coroutine=sleep_run) bot.add_strategies(strategies=strategies) bot.execute() +async def sleep_run(): + while True: + print("Sleeping for 5 seconds") + await asyncio.sleep(5) + print("Hello World") + sample_bot() diff --git a/src/aiomql/lib/executor.py b/src/aiomql/lib/executor.py index 30cbac2..253df38 100644 --- a/src/aiomql/lib/executor.py +++ b/src/aiomql/lib/executor.py @@ -78,9 +78,16 @@ class Executor: async def create_coroutines_task(self): """""" - task = asyncio.create_task(asyncio.gather(*self.coroutines, return_exceptions=True)) + coros = [asyncio.create_task(coroutine) for coroutine in self.coroutines] + # task = asyncio.create_task(asyncio.gather(*coros, return_exceptions=False)) + self.tasks.extend(coros) + # loop = asyncio.get_running_loop() + # loop.run_in_executor() + task = asyncio.gather(*coros, return_exceptions=True) self.tasks.append(task) await task + # await task + # return task def run_coroutine_tasks(self): """Run all coroutines in the executor""" @@ -109,12 +116,19 @@ class Executor: while self.config.shutdown is False and self.config.force_shutdown is False: if self.timeout is not None and self.timeout < (asyncio.get_event_loop().time() - start): self.config.shutdown = True + timeout = self.timeout or 120 + await asyncio.sleep(timeout) + print("Shutting down executor") for strategy in self.strategy_runners: strategy.running = False + self.executor.shutdown(wait=False, cancel_futures=True) + for task in self.tasks: task.cancel() - self.executor.shutdown(wait=False, cancel_futures=True) + + + # self.executor.shutdown(wait=False, cancel_futures=True) if self.config.force_shutdown: os._exit(1) except Exception as err: