Skip to content

Commit 1bf5e15

Browse files
authored
Merge pull request #570 from opentensor/feat/thewhaleking/reconfigure-asyncio-runner
Reconfig Asyncio Runner
2 parents 88a4459 + 5e6992c commit 1bf5e15

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed

bittensor_cli/cli.py

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,17 @@ def __init__(self):
643643
# },
644644
}
645645
self.subtensor = None
646+
647+
if sys.version_info < (3, 10):
648+
# For Python 3.9 or lower
649+
self.event_loop = asyncio.new_event_loop()
650+
else:
651+
try:
652+
uvloop = importlib.import_module("uvloop")
653+
self.event_loop = uvloop.new_event_loop()
654+
except ModuleNotFoundError:
655+
self.event_loop = asyncio.new_event_loop()
656+
646657
self.config_base_path = os.path.expanduser(defaults.config.base_path)
647658
self.config_path = os.path.expanduser(defaults.config.path)
648659

@@ -1097,12 +1108,9 @@ async def _run():
10971108
initiated = False
10981109
try:
10991110
if self.subtensor:
1100-
async with self.subtensor:
1101-
initiated = True
1102-
result = await cmd
1103-
else:
1104-
initiated = True
1105-
result = await cmd
1111+
await self.subtensor.substrate.initialize()
1112+
initiated = True
1113+
result = await cmd
11061114
return result
11071115
except (ConnectionRefusedError, ssl.SSLError, InvalidHandshake):
11081116
err_console.print(f"Unable to connect to the chain: {self.subtensor}")
@@ -1128,12 +1136,13 @@ async def _run():
11281136
exit_early is True
11291137
): # temporarily to handle multiple run commands in one session
11301138
try:
1139+
await self.subtensor.substrate.close()
11311140
raise typer.Exit()
11321141
except Exception as e: # ensures we always exit cleanly
11331142
if not isinstance(e, (typer.Exit, RuntimeError)):
11341143
err_console.print(f"An unknown error has occurred: {e}")
11351144

1136-
return self.asyncio_runner(_run())
1145+
return self.event_loop.run_until_complete(_run())
11371146

11381147
def main_callback(
11391148
self,
@@ -1184,20 +1193,6 @@ def main_callback(
11841193
if k in self.config.keys():
11851194
self.config[k] = v
11861195

1187-
if sys.version_info < (3, 10):
1188-
# For Python 3.9 or lower
1189-
self.asyncio_runner = asyncio.get_event_loop().run_until_complete
1190-
else:
1191-
try:
1192-
uvloop = importlib.import_module("uvloop")
1193-
if sys.version_info >= (3, 11):
1194-
self.asyncio_runner = uvloop.run
1195-
else:
1196-
uvloop.install()
1197-
self.asyncio_runner = asyncio.run
1198-
except ModuleNotFoundError:
1199-
self.asyncio_runner = asyncio.run
1200-
12011196
def verbosity_handler(
12021197
self, quiet: bool, verbose: bool, json_output: bool = False
12031198
) -> None:

0 commit comments

Comments
 (0)