|
| 1 | +--- |
| 2 | +title: "Managing Subtensor Connections" |
| 3 | +--- |
| 4 | + |
| 5 | +# Managing Subtensor Connections |
| 6 | + |
| 7 | +Every time the Bittensor Python SDK's `Subtensor` or `AsyncSubtensor` class is instantiated, it creates a new websocket connection to Subtensor, Bittensor's Blockchain. Your code should ensure that these websocket connections are handled efficiently and closed when no longer in use by your code. |
| 8 | + |
| 9 | + |
| 10 | +If not explicitly closed, connections *should* be terminated by Python's garbage collector, but threading can make this unreliable. (see [this comment](https://github.com/python-websockets/websockets/pull/1601#issuecomment-2705871026).) |
| 11 | + |
| 12 | +Therefore, the best solution is to always use one of the following techniques to close your instance's connection: |
| 13 | + |
| 14 | +1. Use the `close()` method to manually terminate the instance and connection. |
| 15 | +1. **(Preferred)** Use the `with` keyword to instantiate the Subtensor object within a Python [context manager](https://docs.python.org/3/reference/datamodel.html#context-managers). This ensures the websocket connection within the Subtensor instance is terminated gracefully when the context ends. |
| 16 | + |
| 17 | +:::tip |
| 18 | +You don't need to both a `with`-statement context manager and `.close()`, just one or the other. |
| 19 | +::: |
| 20 | + |
| 21 | +## What not to do |
| 22 | + |
| 23 | + |
| 24 | +```python |
| 25 | +import bittensor as bt |
| 26 | +sub = bt.subtensor("finney") |
| 27 | +# calls to subtensor |
| 28 | + |
| 29 | +# no close |
| 30 | +# potential resource leak: |
| 31 | +# the connection is open until/unless garbage collected |
| 32 | +``` |
| 33 | + |
| 34 | + |
| 35 | +## With `close()` |
| 36 | + |
| 37 | +### Sync |
| 38 | +```python |
| 39 | +import bittensor as bt |
| 40 | +sub = bt.subtensor("finney") |
| 41 | +# subtensor calls |
| 42 | +sub.close() |
| 43 | +# instance and connection are terminated. |
| 44 | +``` |
| 45 | + |
| 46 | +### Async |
| 47 | +```python |
| 48 | +import bittensor as bt |
| 49 | +sub = bt.AsyncSubtensor() |
| 50 | +await sub.initialize() |
| 51 | +# calls to subtensor |
| 52 | +await sub.close() |
| 53 | +# instance and connection are terminated. |
| 54 | +``` |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | +## With `with` |
| 59 | + |
| 60 | +### Sync |
| 61 | +```python |
| 62 | +import bittensor as bt |
| 63 | +with bt.subtensor("finney") as sub: |
| 64 | + # all calls to subtensor instance inside this block |
| 65 | + |
| 66 | + |
| 67 | +# instance and connection are terminated at the end of the context scope |
| 68 | +``` |
| 69 | +### Async |
| 70 | + |
| 71 | +```python |
| 72 | +import bittensor as bt |
| 73 | +async with bt.AsyncSubtensor() as sub: |
| 74 | + # calls to subtensor |
| 75 | + |
| 76 | +# instance and connection are terminated at the end of the context scope |
| 77 | +``` |
| 78 | +or |
| 79 | +```python |
| 80 | +import bittensor as bt |
| 81 | +sub = bt.AsyncSubtensor() |
| 82 | +async with sub: |
| 83 | + # calls to subtensor |
| 84 | + |
| 85 | +# instance and connection are terminated at the end of the context scope |
| 86 | +``` |
| 87 | + |
0 commit comments