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
This commit is contained in:
The MMGen Project 2025-10-01 15:30:57 +00:00
commit a3e7c08f84
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
6 changed files with 18 additions and 16 deletions

View file

@ -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

View file

@ -1 +1 @@
September 2025
October 2025

View file

@ -1 +1 @@
16.1.dev2
16.1.dev3

View file

@ -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

View file

@ -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:

View file

@ -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