From a3e7c08f8464676be4d1ffac6be01f404907417e Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Wed, 1 Oct 2025 15:30:57 +0000 Subject: [PATCH] implement `async_run()` with `aiohttp` backend - aiohttp is now used in the proper context-manager way, so we can remove the package version pin in setup.cfg --- mmgen/cfg.py | 3 ++- mmgen/data/release_date | 2 +- mmgen/data/version | 2 +- mmgen/rpc/backends/aiohttp.py | 13 ++----------- mmgen/util.py | 12 +++++++++++- setup.cfg | 2 +- 6 files changed, 18 insertions(+), 16 deletions(-) diff --git a/mmgen/cfg.py b/mmgen/cfg.py index 200d2c8c..7835b0ff 100755 --- a/mmgen/cfg.py +++ b/mmgen/cfg.py @@ -147,7 +147,7 @@ class Config(Lockable): 3 - config file """ _autolock = False - _set_ok = ('usr_randchars', '_proto') + _set_ok = ('usr_randchars', '_proto', 'aiohttp_session') _reset_ok = ('accept_defaults',) _delete_ok = ('_opts',) _use_class_attr = True @@ -200,6 +200,7 @@ class Config(Lockable): rpc_user = '' rpc_password = '' aiohttp_rpc_queue_len = 16 + aiohttp_session = None cached_balances = False # daemons diff --git a/mmgen/data/release_date b/mmgen/data/release_date index 38ae3177..4d3e4402 100644 --- a/mmgen/data/release_date +++ b/mmgen/data/release_date @@ -1 +1 @@ -September 2025 +October 2025 diff --git a/mmgen/data/version b/mmgen/data/version index 5c3dc5a1..05b13e49 100644 --- a/mmgen/data/version +++ b/mmgen/data/version @@ -1 +1 @@ -16.1.dev2 +16.1.dev3 diff --git a/mmgen/rpc/backends/aiohttp.py b/mmgen/rpc/backends/aiohttp.py index 59c8a1e2..e53e9c55 100755 --- a/mmgen/rpc/backends/aiohttp.py +++ b/mmgen/rpc/backends/aiohttp.py @@ -28,20 +28,11 @@ class aiohttp(base, metaclass=AsyncInit): variables (all are case insensitive). """ - def __del__(self): - self.connector.close() - self.session.detach() - del self.session - async def __init__(self, caller): super().__init__(caller) - import aiohttp - self.connector = aiohttp.TCPConnector(limit_per_host=self.cfg.aiohttp_rpc_queue_len) - self.session = aiohttp.ClientSession( - headers = {'Content-Type': 'application/json'}, - connector = self.connector, - ) + self.session = self.cfg.aiohttp_session if caller.auth_type == 'basic': + import aiohttp self.auth = aiohttp.BasicAuth(*caller.auth, encoding='UTF-8') else: self.auth = None diff --git a/mmgen/util.py b/mmgen/util.py index c0b40b5f..8bbc5a33 100755 --- a/mmgen/util.py +++ b/mmgen/util.py @@ -443,7 +443,17 @@ def get_subclasses(cls, *, names=False): def async_run(cfg, func, *, args=(), kwargs={}): import asyncio - return asyncio.run(func(*args, **kwargs)) + if cfg.rpc_backend == 'aiohttp': + async def func2(): + import aiohttp + connector = aiohttp.TCPConnector(limit_per_host=cfg.aiohttp_rpc_queue_len) + async with aiohttp.ClientSession( + headers = {'Content-Type': 'application/json'}, + connector = connector) as cfg.aiohttp_session: + return await func(*args, **kwargs) + return asyncio.run(func2()) + else: + return asyncio.run(func(*args, **kwargs)) def wrap_ripemd160(called=[]): if not called: diff --git a/setup.cfg b/setup.cfg index a208dcd4..4bce54b5 100644 --- a/setup.cfg +++ b/setup.cfg @@ -62,7 +62,7 @@ install_requires = cryptography pynacl ecdsa - aiohttp==3.12.9 # in later versions, TCPConnector.close() is a coroutine + aiohttp requests pexpect lxml