@@ -25,7 +25,9 @@ It is likely preferable to use the official `Container` class, which provides he
2525a more idiomatic API for working with containers on top of Durable Objects.
2626:::
2727
28- <TypeScriptExample filename = " index.ts" >
28+ <Tabs >
29+
30+ <TypeScriptExample filename = " index.ts" omitTabs = { true } >
2931``` ts
3032export class MyDurableObject extends DurableObject {
3133 constructor (ctx : DurableObjectState , env : Env ) {
@@ -42,24 +44,63 @@ export class MyDurableObject extends DurableObject {
4244```
4345</TypeScriptExample >
4446
47+ <TabItem label = " Python" icon = " seti:python" >
48+
49+ ``` python
50+ from workers import DurableObject
51+
52+ class MyDurableObject (DurableObject ):
53+ def __init__ (self , ctx , env ):
54+ super ().__init__ (ctx, env)
55+
56+ # boot the container when starting the DO
57+ async def start_container ():
58+ self .ctx.container.start()
59+ self .ctx.blockConcurrencyWhile(start_container)
60+ ```
61+
62+ </TabItem >
63+
64+ </Tabs >
65+
4566
4667## Attributes
4768
4869### ` running `
4970
5071` running ` returns ` true ` if the container is currently running. It does not ensure that the container has fully started and ready to accept requests.
5172
73+ <Tabs >
74+
75+ <TabItem label = " JavaScript" icon = " seti:javascript" >
76+
5277``` js
53- this .ctx .container .running ;
78+ this .ctx .container .running ;
79+ ```
80+
81+ </TabItem >
82+
83+ <TabItem label = " Python" icon = " seti:python" >
84+
85+ ``` python
86+ self .ctx.container.running
5487```
5588
89+ </TabItem >
90+
91+ </Tabs >
92+
5693## Methods
5794
5895### ` start `
5996
6097` start ` boots a container. This method does not block until the container is fully started.
6198You may want to confirm the container is ready to accept requests before using it.
6299
100+ <Tabs >
101+
102+ <TabItem label = " JavaScript" icon = " seti:javascript" >
103+
63104``` js
64105this .ctx .container .start ({
65106 env: {
@@ -70,6 +111,26 @@ this.ctx.container.start({
70111});
71112```
72113
114+ </TabItem >
115+
116+ <TabItem label = " Python" icon = " seti:python" >
117+
118+ ``` python
119+ from pyodide.ffi import to_js
120+
121+ self .ctx.container.start(
122+ env = to_js({
123+ " FOO" : " bar" ,
124+ }),
125+ enableInternet = False ,
126+ entrypoint = to_js([" node" , " server.js" ]),
127+ )
128+ ```
129+
130+ </TabItem >
131+
132+ </Tabs >
133+
73134#### Parameters
74135
75136- ` options ` (optional): An object with the following properties:
@@ -85,10 +146,26 @@ this.ctx.container.start({
85146
86147` destroy ` stops the container and optionally returns a custom error message to the ` monitor() ` error callback.
87148
149+ <Tabs >
150+
151+ <TabItem label = " JavaScript" icon = " seti:javascript" >
152+
88153``` js
89154this .ctx .container .destroy (" Manually Destroyed" );
90155```
91156
157+ </TabItem >
158+
159+ <TabItem label = " Python" icon = " seti:python" >
160+
161+ ``` python
162+ self .ctx.container.destroy(" Manually Destroyed" )
163+ ```
164+
165+ </TabItem >
166+
167+ </Tabs >
168+
92169#### Parameters
93170
94171- ` error ` (optional): A string that will be sent to the error handler of the ` monitor ` method. This is useful for logging or debugging purposes.
@@ -101,11 +178,28 @@ this.ctx.container.destroy("Manually Destroyed");
101178
102179` signal ` sends an IPC signal to the container, such as SIGKILL or SIGTERM. This is useful for stopping the container gracefully or forcefully.
103180
181+ <Tabs >
182+
183+ <TabItem label = " JavaScript" icon = " seti:javascript" >
184+
104185``` js
105186const SIGTERM = 15 ;
106187this .ctx .container .signal (SIGTERM );
107188```
108189
190+ </TabItem >
191+
192+ <TabItem label = " Python" icon = " seti:python" >
193+
194+ ``` python
195+ SIGTERM = 15
196+ self .ctx.container.signal(SIGTERM )
197+ ```
198+
199+ </TabItem >
200+
201+ </Tabs >
202+
109203#### Parameters
110204
111205- ` signal ` : a number representing the signal to send to the container. This is typically a POSIX signal number, such as SIGTERM (15) or SIGKILL (9).
@@ -118,6 +212,10 @@ this.ctx.container.signal(SIGTERM);
118212
119213` getTcpPort ` returns a TCP port from the container. This can be used to communicate with the container over TCP and HTTP.
120214
215+ <Tabs >
216+
217+ <TabItem label = " JavaScript" icon = " seti:javascript" >
218+
121219``` js
122220const port = this .ctx .container .getTcpPort (8080 );
123221const res = await port .fetch (" http://container/set-state" , {
@@ -141,6 +239,38 @@ try {
141239}
142240```
143241
242+ </TabItem >
243+
244+ <TabItem label = " Python" icon = " seti:python" >
245+
246+ ``` python
247+ port = self .ctx.container.getTcpPort(8080 )
248+ res = await port.fetch(
249+ " http://container/set-state" ,
250+ body = initial_state,
251+ method = " POST" ,
252+ )
253+ ```
254+
255+ ``` python
256+ from workers import Response
257+
258+ conn = self .ctx.container.getTcpPort(8080 ).connect(' 10.0.0.1:8080' )
259+ await conn.opened
260+
261+ try :
262+ if request.body:
263+ await request.body.pipeTo(conn.writable)
264+ return Response(conn.readable)
265+ except Exception as err:
266+ print (f " Request body piping failed: { err} " )
267+ return Response(" Failed to proxy request body" , status = 502 )
268+ ```
269+
270+ </TabItem >
271+
272+ </Tabs >
273+
144274#### Parameters
145275
146276- ` port ` (number): a TCP port number to use for communication with the container.
@@ -154,6 +284,10 @@ try {
154284` monitor ` returns a promise that resolves when a container exits and errors if a container errors. This is useful for setting up
155285callbacks to handle container status changes in your Workers code.
156286
287+ <Tabs >
288+
289+ <TabItem label = " JavaScript" icon = " seti:javascript" >
290+
157291``` js
158292class MyContainer extends DurableObject {
159293 constructor (ctx , env ) {
@@ -173,6 +307,32 @@ class MyContainer extends DurableObject {
173307}
174308```
175309
310+ </TabItem >
311+
312+ <TabItem label = " Python" icon = " seti:python" >
313+
314+ ``` python
315+ from workers import DurableObject
316+
317+ class MyContainer (DurableObject ):
318+ def __init__ (self , ctx , env ):
319+ super ().__init__ (ctx, env)
320+
321+ def on_container_exit ():
322+ print (" Container exited" )
323+
324+ # the "err" value can be customized by the destroy() method
325+ async def on_container_error (err ):
326+ print (f " Container errored: { err} " )
327+
328+ self .ctx.container.start()
329+ self .ctx.container.monitor().then(on_container_exit).catch(on_container_error)
330+ ```
331+
332+ </TabItem >
333+
334+ </Tabs >
335+
176336#### Parameters
177337
178338- None
0 commit comments