Skip to content

fix: replace deprecated asyncio.get_event_loop() and deduplicate network init#395

Open
t7929375-eng wants to merge 1 commit intoentrius:testfrom
t7929375-eng:fix/replace-deprecated-asyncio-get-event-loop
Open

fix: replace deprecated asyncio.get_event_loop() and deduplicate network init#395
t7929375-eng wants to merge 1 commit intoentrius:testfrom
t7929375-eng:fix/replace-deprecated-asyncio-get-event-loop

Conversation

@t7929375-eng
Copy link
Copy Markdown

Problem

Both miner_post and miner_check CLI commands use asyncio.get_event_loop().run_until_complete() which is deprecated since Python 3.10 (emits DeprecationWarning) and raises RuntimeError in Python 3.12+ when no running event loop exists.

Additionally, both commands duplicate the bittensor initialization block (wallet, subtensor, metagraph, dendrite) — once inside console.status() and once outside for JSON mode — making the code fragile and harder to maintain.

Solution

  1. Replace asyncio.get_event_loop().run_until_complete(coro) with asyncio.run(async_fn()) — the modern equivalent that properly creates and manages the event loop.
  2. Extract _connect() helper to eliminate the duplicated 4-line init block in each command.
  3. Extract _broadcast() / _check() async helpers to avoid repeating the dendrite call.

Changes

post.py

  • Extract _connect() for wallet/subtensor/metagraph/dendrite init (−8 duplicated lines)
  • Extract _broadcast() coroutine, replace 2× asyncio.get_event_loop().run_until_complete()

check.py

  • Same _connect() extraction (−8 duplicated lines)
  • Extract _check() coroutine, replace 2× asyncio.get_event_loop().run_until_complete()

Net result: +38 / −48 lines (code reduction).

Type of Change

  • Bug fix
  • Refactor

Testing

  • ruff check passes
  • ruff format passes
  • pyright passes (0 errors)
  • Self-reviewed

Checklist

  • Code follows project style guidelines
  • Self-review completed

cc @anderdc @LandynDev

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant