Open
Description
In example python code, we should be using explicit contexts for subtensor instances to avoid connection/memory leaks from leaving these instances open.
Subtensor must be initialized either with keyword with to create a cope, or explicitly closed with close()
If you don't do this correctly, it leaves the websocket open. we do attempt to close the websocket upon garbage collection, but because it's threaded, this is unreliable (see: python-websockets/websockets#1601 (comment))
You don't need to use the context manager and .close() — just one or the other. This is fine:
@thewhaleking for review
import bittensor as bt
with bt.subtensor("finney") as sub:
# all calls to subtensor instance inside this block
This is also fine, though uglier:
import bittensor as bt
sub = bt.subtensor("finney")
# subtensor calls
sub.close()
This is not fine:
import bittensor as bt
sub = bt.subtensor("finney")
# calls to subtensor
# no close
With async, it's the same. This is fine:
import bittensor as bt
async with bt.AsyncSubtensor() as sub:
# calls to subtensor
or
import bittensor as bt
sub = bt.AsyncSubtensor()
async with sub:
# calls to subtensor
or the uglier:
import bittensor as bt
sub = bt.AsyncSubtensor()
await sub.initialize()
# calls to subtensor
await sub.close()
Metadata
Metadata
Assignees
Labels
No labels