From 8e0a5516774894505d8d915bbd510bf88c6e3e48 Mon Sep 17 00:00:00 2001 From: Ichinga Samuel Date: Sun, 29 Jun 2025 18:39:15 +0100 Subject: [PATCH] Modify process_poll classmethod in the Bot class Add process_poll function in a process_poll module Modify documentation for process_poll --- src/aiomql/__init__.py | 1 + src/aiomql/lib/bot.py | 13 ++++++------- src/aiomql/process_pool.py | 16 ++++++++++++++++ 3 files changed, 23 insertions(+), 7 deletions(-) create mode 100644 src/aiomql/process_pool.py diff --git a/src/aiomql/__init__.py b/src/aiomql/__init__.py index 136d3d0..f35d8ee 100644 --- a/src/aiomql/__init__.py +++ b/src/aiomql/__init__.py @@ -2,3 +2,4 @@ from .core import * from .lib import * from .contrib import * from ._utils import * +from .process_pool import process_pool diff --git a/src/aiomql/lib/bot.py b/src/aiomql/lib/bot.py index f1bab00..998830c 100644 --- a/src/aiomql/lib/bot.py +++ b/src/aiomql/lib/bot.py @@ -21,7 +21,6 @@ class Bot: config (Config): Config instance mt (MetaTrader): MetaTrader instance """ - config: Config executor: Executor mt: MetaTrader @@ -34,17 +33,17 @@ class Bot: self.strategies = [] @classmethod - def process_pool(cls, bots: dict[Callable:dict] = None, num_workers: int = None): - """Run multiple bots in parallel using a ProcessPoolExecutor. Each bot should be a callable that accepts + def process_pool(cls, processes: dict[Callable:dict] = None, num_workers: int = None): + """Run multiple processes in parallel using a ProcessPoolExecutor. Each bot should be a callable that accepts keyword arguments only. Args: - bots (dict): A dictionary of bots to run with their respective keyword arguments as a dictionary - num_workers (int): Number of workers to run the bots + processes (dict): A dictionary of processes to run with their respective keyword arguments as a dictionary + num_workers (int): Number of workers to run the processes """ - num_workers = num_workers or len(bots) + 1 + num_workers = num_workers or len(processes) + 1 with ProcessPoolExecutor(max_workers=num_workers) as executor: - for bot, kwargs in bots.items(): + for bot, kwargs in processes.items(): executor.submit(bot, **kwargs) async def initialize(self): diff --git a/src/aiomql/process_pool.py b/src/aiomql/process_pool.py new file mode 100644 index 0000000..af8b762 --- /dev/null +++ b/src/aiomql/process_pool.py @@ -0,0 +1,16 @@ +from typing import Callable +from concurrent.futures import ProcessPoolExecutor + + +def process_pool(processes: dict[Callable:dict] = None, num_workers: int = None): + """Run multiple processes in parallel using a ProcessPoolExecutor. Each bot should be a callable that accepts + keyword arguments only. + + Args: + processes (dict): A dictionary of processes to run with their respective keyword arguments as a dictionary + num_workers (int): Number of workers to run the processes + """ + num_workers = num_workers or len(processes) + 1 + with ProcessPoolExecutor(max_workers=num_workers) as executor: + for bot, kwargs in processes.items(): + executor.submit(bot, **kwargs)