PageSourceSearch

https://developer-docs-6uq.pages.dev/assets/js/135ec820.656791e4.js

js developer-docs-6uq.pages.dev collected 2026-10-03 06:42:44 UTC 12,193 bytes, 1 lines download raw bytes

1"use strict";(self.webpackChunkbittensor_docs=self.webpackChunkbittensor_docs||[]).push([[8986],{481:(n,e,t)=>{t.d(e,{b9:()=>s});var o=t(5277),i=t(4073);const s=()=>(0,o.jsx)(i.A,{type:"info",title:"SDK Version",children:(0,o.jsxs)("p",{children:["This documentation is built against ",(0,o.jsx)("strong",{children:"Bittensor Python SDK v10"}),". For documentation built against SDK 9.12, see ",(0,o.jsx)("a",{href:"https://archive-sdk-9-12.developer-docs-6uq.pages.dev/sdk/bt-api-ref",target:"_blank",rel:"noopener noreferrer",children:"SDK 9.12 Archive"}),"."]})})},771:(n,e,t)=>{t.d(e,{R:()=>r,x:()=>c});var o=t(8521);const i={},s=o.createContext(i);function r(n){const e=o.useContext(s);return o.useMemo((function(){return"function"==typeof n?n(e):{...e,...n}}),[e,n])}function c(n){let e;return e=n.disableParentContext?"function"==typeof n.components?n.components(i):n.components||i:r(n.components),o.createElement(s.Provider,{value:e},n.children)}},1118:(n,e,t)=>{t.r(e),t.d(e,{assets:()=>l,contentTitle:()=>a,default:()=>u,frontMatter:()=>c,metadata:()=>o,toc:()=>d});const o=JSON.parse('{"id":"sdk/managing-subtensor-connections","title":"Managing Subtensor Connections","description":"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.","source":"@site/docs/sdk/managing-subtensor-connections.md","sourceDirName":"sdk","slug":"/sdk/managing-subtensor-connections","permalink":"/sdk/managing-subtensor-connections","draft":false,"unlisted":false,"editUrl":"https://github.com/latent-to/developer-docs/blob/main/docs/sdk/managing-subtensor-connections.md","tags":[],"version":"current","lastUpdatedAt":1785767577000,"frontMatter":{"title":"Managing Subtensor Connections"},"sidebar":"bittensorSidebar","previous":{"title":"Working with Concurrency","permalink":"/subnets/asyncio"},"next":{"title":"Subtensor API","permalink":"/subtensor-api/"}}');var i=t(5277),s=t(771),r=t(481);const c={title:"Managing Subtensor Connections"},a=void 0,l={},d=[{value:"What not to do",id:"what-not-to-do",level:2},{value:"With <code>close()</code>",id:"with-close",level:2},{value:"Sync",id:"sync",level:3},{value:"Async",id:"async",level:3},{value:"With <code>with</code>",id:"with-with",level:2},{value:"Sync",id:"sync-1",level:3},{value:"Async",id:"async-1",level:3}];function h(n){const e={a:"a",admonition:"admonition",code:"code",em:"em",h2:"h2",h3:"h3",li:"li",ol:"ol",p:"p",pre:"pre",strong:"strong",...(0,s.R)(),...n.components};return(0,i.jsxs)(i.Fragment,{children:[(0,i.jsxs)(e.p,{children:["Every time the Bittensor Python SDK's ",(0,i.jsx)(e.code,{children:"Subtensor"})," or ",(0,i.jsx)(e.code,{children:"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."]}),"\n",(0,i.jsxs)(e.p,{children:["If not explicitly closed, connections ",(0,i.jsx)(e.em,{children:"should"})," be terminated by Python's garbage collector, but threading can make this unreliable. (see ",(0,i.jsx)(e.a,{href:"https://github.com/python-websockets/websockets/pull/1601#issuecomment-2705871026",children:"this comment"}),".)"]}),"\n",(0,i.jsx)(e.p,{children:"Therefore, the best solution is to always use one of the following techniques to close your instance's connection:"}),"\n",(0,i.jsxs)(e.ol,{children:["\n",(0,i.jsxs)(e.li,{children:["Use the ",(0,i.jsx)(e.code,{children:"close()"})," method to manually terminate the instance and connection."]}),"\n",(0,i.jsxs)(e.li,{children:[(0,i.jsx)(e.strong,{children:"(Preferred)"})," Use the ",(0,i.jsx)(e.code,{children:"with"})," keyword to instantiate the Subtensor object within a Python ",(0,i.jsx)(e.a,{href:"https://docs.python.org/3/reference/datamodel.html#context-managers",children:"context manager"}),". This ensures the websocket connection within the Subtensor instance is terminated gracefully when the context ends."]}),"\n"]}),"\n",(0,i.jsx)(e.admonition,{type:"tip",children:(0,i.jsxs)(e.p,{children:["You don't need to both a ",(0,i.jsx)(e.code,{children:"with"}),"-statement context manager and ",(0,i.jsx)(e.code,{children:".close()"}),", just one or the other."]}
1)}),"\n",(0,i.jsx)(e.h2,{id:"what-not-to-do",children:"What not to do"}),"\n",(0,i.jsx)(r.b9,{}),"\n",(0,i.jsx)(e.pre,{children:(0,i.jsx)(e.code,{className:"language-python",children:'import bittensor as bt\nsub = bt.Subtensor("finney")\n# calls to subtensor\n\n# no close\n# potential resource leak:\n# the connection is open until/unless garbage collected\n'})}),"\n",(0,i.jsxs)(e.h2,{id:"with-close",children:["With ",(0,i.jsx)(e.code,{children:"close()"})]}),"\n",(0,i.jsx)(e.h3,{id:"sync",children:"Sync"}),"\n",(0,i.jsx)(e.pre,{children:(0,i.jsx)(e.code,{className:"language-python",children:'import bittensor as bt\nsub = bt.Subtensor("finney")\n# subtensor calls\nsub.close()\n# instance and connection are terminated.\n'})}),"\n",(0,i.jsx)(e.h3,{id:"async",children:"Async"}),"\n",(0,i.jsx)(e.pre,{children:(0,i.jsx)(e.code,{className:"language-python",children:"import bittensor as bt\nsub = bt.AsyncSubtensor()\nawait sub.initialize()\n# calls to subtensor\nawait sub.close()\n# instance and connection are terminated.\n"})}),"\n",(0,i.jsxs)(e.h2,{id:"with-with",children:["With ",(0,i.jsx)(e.code,{children:"with"})]}),"\n",(0,i.jsx)(e.h3,{id:"sync-1",children:"Sync"}),"\n",(0,i.jsx)(e.pre,{children:(0,i.jsx)(e.code,{className:"language-python",children:'import bittensor as bt\nwith bt.Subtensor("finney") as sub:\n    # all calls to subtensor instance inside this block\n\n\n# instance and connection are terminated at the end of the context scope\n'})}),"\n",(0,i.jsx)(e.h3,{id:"async-1",children:"Async"}),"\n",(0,i.jsx)(e.pre,{children:(0,i.jsx)(e.code,{className:"language-python",children:"import bittensor as bt\nasync with bt.AsyncSubtensor() as sub:\n    # calls to subtensor\n\n# instance and connection are terminated at the end of the context scope\n"})}),"\n",(0,i.jsx)(e.p,{children:"or"}),"\n",(0,i.jsx)(e.pre,{children:(0,i.jsx)(e.code,{className:"language-python",children:"import bittensor as bt\nsub = bt.AsyncSubtensor()\nasync with sub:\n    # calls to subtensor\n\n# instance and connection are terminated at the end of the context scope\n"})})]})}function u(n={}){const{wrapper:e}={...(0,s.R)(),...n.components};return e?(0,i.jsx)(e,{...n,children:(0,i.jsx)(h,{...n})}):h(n)}},1361:(n,e,t)=>{t.d(e,{A:()=>i});t(8521);var o=t(5277);function i(n){return(0,o.jsx)("svg",{viewBox:"0 0 24 24",fill:"currentColor","aria-hidden":"true",...n,children:(0,o.jsx)("path",{d:"M8.27,3L3,8.27V15.73L8.27,21H15.73L21,15.73V8.27L15.73,3M8.27,5H15.73L19,8.27V15.73L15.73,19H8.27L5,15.73V8.27M11,15H13V17H11V15M11,7H13V13H11V7"})})}},2165:(n,e,t)=>{t.d(e,{A:()=>i});t(8521);var o=t(5277);function i(n){return(0,o.jsx)("svg",{viewBox:"0 0 24 24",fill:"currentColor","aria-hidden":"true",...n,children:(0,o.jsx)("path",{d:"M12,2A7,7 0 0,1 19,9C19,11.38 17.81,13.47 16,14.74V17A1,1 0 0,1 15,18H9A1,1 0 0,1 8,17V14.74C6.19,13.47 5,11.38 5,9A7,7 0 0,1 12,2M9,21V20H15V21A1,1 0 0,1 14,22H10A1,1 0 0,1 9,21M12,4A5,5 0 0,0 7,9C7,10.82 8.03,12.42 9.54,13.31L10,13.58V16H14V13.58L14.46,13.31C15.97,12.42 17,10.82 17,9A5,5 0 0,0 12,4Z"})})}},3334:(n,e,t)=>{t.d(e,{A:()=>i});t(8521);var o=t(5277);function i(n){return(0,o.jsx)("svg",{viewBox:"0 0 24 24",fill:"currentColor","aria-hidden":"true",...n,children:(0,o.jsx)("path",{d:"M14.06,9L15,9.94L5.92,19H5V18.08L14.06,9M17.66,3C17.41,3 17.15,3.1 16.96,3.29L15.13,5.12L18.88,8.87L20.71,7.04C21.1,6.65 21.1,6 20.71,5.63L18.37,3.29C18.17,3.09 17.92,3 17.66,3M14.06,6.19L3,17.25V21H6.75L17.81,9.94L14.06,6.19Z"})})}},4073:(n,e,t)=>{t.d(e,{A:()=>k});var o=t(8521),i=t(5277);function s(n){const{mdxAdmonitionTitle:e,rest:t}=function(n){const e=o.Children.toArray(n),t=e.find((n=>o.isValidElement(n)&&"mdxAdmonitionTitle"===n.type)),s=e.filter((n=>n!==t)),r=t?.props.children;return{mdxAdmonitionTitle:r,rest:s.length>0?(0,i.jsx)(i.Fragment,{children:s}):null}}(n.children),s=n.title??e;return{...n,...s&&{title:s},children:t}}var r=t(613),c=t(5127),a=t(4855),l=t(3334);const d={icon:(0,i.jsx)(l.A,{}),title:(0,i.jsx)(c.A,{id:"theme.admonition.note",description:"The default label used for the Note admonition (:::note)",children:"note"})};function h(n){return(0,i.jsx)(a.A,{...d,...n,className:(0,r.A)("alert alert--secondary",n.className),children:n.children})}var u=t(2165);const m={icon:(0,i.jsx)(u.A,{}),title:(0,i.jsx)(c.A,{id:"theme.admonition.tip",description:"The default label used for the Tip admonition (:::tip)",children:"tip"})};function x(n){return(0,i.jsx)(a.A,{...m,...n,className:(0,r.A)("alert alert--success",n.className),children:n.children})}var b=t(6518);const p={icon:(0,i.jsx)(b.A,{}),title:(0,i.jsx)(c.A,{id:"theme.admonition.info",description:"The default label used for the Info admonition (:::info)",children:"info"})};function j(n){return(0,i.jsx)(a.A,{...p,...n,className:(0,r.A)("alert alert--info",n.className),children:n.children})}var f=t(6396);const g={icon:(0,i.jsx)(f.A,{}),title:(0,i.jsx)(c.A,{id:"theme.admonition.warning",description:"The default label used for the Warning admonition (:::warning)",children:"warning"})};var y=t(1361);const A={icon:(0,i.jsx)(y.A,{}),title:(0,i.jsx)(c.A,{id:"theme.admonition.danger",description:"The default label used for the Danger admonition (:::danger)",children:"danger"})};const v={icon:(0,i.jsx)(f.A,{}),title:(0,i.jsx)(c.A,{id:"theme.admonition.caution",description:"The default label used for the Caution admonition (:::caution)",children:"caution"})};const w={...{note:h,tip:x,info:j,warning:function(n){return(0,i.jsx)(a.A,{...g,...n,className:(0,r.A)("alert alert--warning",n.className),children:n.children})},danger:function(n){return(0,i.jsx)(a.A,{...A,...n,className:(0,r.A)("alert alert--danger",n.className),children:n.children})}},...{secondary:n=>(0,i.jsx)(h,{title:"secondary",...n}),important:n=>(0,i.jsx)(j,{title:"important",...n}),success:n=>(0,i.jsx)(x,{title:"success",...n}),caution:function(n){return(0,i.jsx)(a.A,{...v,...n,className:(0,r.A)("alert alert--warning",n.className),children:n.children})}}};function k(n){const e=s(n),t=(o=e.type,w[o]||(console.warn(`No admonition component found for admonition type "${o}". Using Info as fallback.`),w.info));var o;return(0,i.jsx)(t,{...e})}},4855:(n,e,t)=>{t.d(e,{A:()=>d});t(8521);var o=t(9257),i=t(1904);const s={admonition:"admonition_Gfwi",admonitionHeading:"admonitionHeading_f1Ed",admonitionIcon:"admonitionIcon_kpSf",admonitionContent:"admonitionContent_UjKb"};var r=t(5277);function c({type:n,className:e,children:t}){return(0,r.jsx)("div",{className:(0,o.A)(i.G.common.admonition,i.G.common.admonitionType(n),s.admonition,e),
1children:t})}function a({icon:n,title:e,hasChildren:t}){return(0,r.jsxs)("div",{className:s.admonitionHeading,children:[(0,r.jsx)("span",{className:s.admonitionIcon,children:n}),(0,r.jsx)("span",{className:(0,o.A)({admonition__heading:t}),children:e})]})}function l({children:n}){return n?(0,r.jsx)("div",{className:s.admonitionContent,children:n}):null}function d(n){const{type:e,icon:t,title:o,children:i,className:s}=n;return(0,r.jsxs)(c,{type:e,className:s,children:[(0,r.jsx)(a,{title:o,icon:t,hasChildren:!!i}),(0,r.jsx)(l,{children:i})]})}},6396:(n,e,t)=>{t.d(e,{A:()=>i});t(8521);var o=t(5277);function i(n){return(0,o.jsx)("svg",{viewBox:"0 0 24 24",fill:"currentColor","aria-hidden":"true",...n,children:(0,o.jsx)("path",{d:"M12,2L1,21H23M12,6L19.53,19H4.47M11,10V14H13V10M11,16V18H13V16"})})}},6518:(n,e,t)=>{t.d(e,{A:()=>i});t(8521);var o=t(5277);function i(n){return(0,o.jsx)("svg",{viewBox:"0 0 24 24",fill:"currentColor","aria-hidden":"true",...n,children:(0,o.jsx)("path",{d:"M11,9H13V7H11M12,20C7.59,20 4,16.41 4,12C4,7.59 7.59,4 12,4C16.41,4 20,7.59 20,12C20,16.41 16.41,20 12,20M12,2A10,10 0 0,0 2,12A10,10 0 0,0 12,22A10,10 0 0,0 22,12A10,10 0 0,0 12,2M11,17H13V11H11V17Z"})})}}}]);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.