Browse Source

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
The MMGen Project 2 months ago
parent
commit
a3e7c08f84
6 changed files with 18 additions and 16 deletions
  1. 2 1
      mmgen/cfg.py
  2. 1 1
      mmgen/data/release_date
  3. 1 1
      mmgen/data/version
  4. 2 11
      mmgen/rpc/backends/aiohttp.py
  5. 11 1
      mmgen/util.py
  6. 1 1
      setup.cfg

+ 2 - 1
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

+ 1 - 1
mmgen/data/release_date

@@ -1 +1 @@
-September 2025
+October 2025

+ 1 - 1
mmgen/data/version

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

+ 2 - 11
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

+ 11 - 1
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:

+ 1 - 1
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