Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
Add example of running background process to the docs
Tomas Valenta7eeff595d58b
diff --git a/apps/docs/src/app/getting-started/sdk-multiple-processes/page.mdx b/apps/docs/src/app/getting-started/sdk-multiple-processes/page.mdxnew file mode 100644index 000000000..b0dc60e1f--- /dev/null+++ b/apps/docs/src/app/getting-started/sdk-multiple-processes/page.mdx@@ -0,0 +1,18 @@+export const metadata = {+ title: 'E2B Documentation',+ description: 'SDK Multiple Processes',+}++# Multiple Processes++The SDK supports starting multiple processes in the playground. You don't have to use any special parameters to enable this.++## Background Processes++If you want to start a long running process (for example a server) in the background you should use `session.process.start` to start the process but after that you should not `await` for the process to finish.++After you start this background process you can continue to use the SDK.++At any time you can kill the background process by calling the `kill()` method on the `Process` object and then retrieve the output of the process by accessing the `output` property of the `Process` object.++<CodeGroupAutoload path="processes/background_processes" />diff --git a/apps/docs/src/code/js/processes/background_processes.js b/apps/docs/src/code/js/processes/background_processes.jsnew file mode 100644index 000000000..557894eb5--- /dev/null+++ b/apps/docs/src/code/js/processes/background_processes.js@@ -0,0 +1,36 @@+import { Session } from '@e2b/sdk'++const session = await Session.create({+ id: 'Python3',+ apiKey: process.env.E2B_API_KEY,+})++function sleep(ms) {+ return new Promise(resolve => setTimeout(resolve, ms));+}++// Start a server process in the background+// We are not calling `await backgroundServer` - that would wait for the process to finish running+const backgroundServer = await session.process.start({ // $HighlightLine+ cmd: 'python3 -m http.server 8000', // $HighlightLine+ onStdout: output => console.log(output), // $HighlightLine+}) // $HighlightLine++// Wait for the server to be accessible+await sleep(1000)++// Start another process that creates a request to server+const serverRequest = await session.process.start({ cmd: 'curl localhost:8000' })++// Wait for the server request to finish running+const requestOutput = await serverRequest.finished++// Stops the background process (it would otherwise run indefinitely). After this the backgroundServer.output should be populated with stdout and stderr that were outputted until the server was killed+await backgroundServer.kill() // $HighlightLine++// Access the server output after the server process is killed+const serverOutput = backgroundServer.output // $HighlightLine++console.log(requestOutput)++await session.close()diff --git a/apps/docs/src/code/js/run_all.js b/apps/docs/src/code/js/run_all.jsindex 9521a1c86..0c64557e0 100644--- a/apps/docs/src/code/js/run_all.js+++ b/apps/docs/src/code/js/run_all.js@@ -16,5 +16,6 @@ import * as process_write_stdin from './basics/process_write_stdin.js'import * as timeout_session from './timeout/timeout_session.js'import * as timeout_process from './timeout/timeout_process.js'import * as timeout_filesystem from './timeout/timeout_filesystem.js'+import * as background_processes from './processes/background_processes.js'console.log('Done')diff --git a/apps/docs/src/code/python/_run_all.py b/apps/docs/src/code/python/_run_all.pyindex e89423ec2..69b2d4e8a 100644--- a/apps/docs/src/code/python/_run_all.py+++ b/apps/docs/src/code/python/_run_all.py@@ -16,5 +16,7 @@ from basics.process_write_stdin import main as process_write_stdin_mainfrom timeout.timeout_session import main as timeout_session_mainfrom timeout.timeout_process import main as timeout_process_mainfrom timeout.timeout_filesystem import main as timeout_filesystem_main+from processes.background_processes import main as background_processes_main+print("Done")\ No newline at end of filediff --git a/apps/docs/src/code/python/processes/background_processes.py b/apps/docs/src/code/python/processes/background_processes.pynew file mode 100644index 000000000..243a9c65d--- /dev/null+++ b/apps/docs/src/code/python/processes/background_processes.py@@ -0,0 +1,40 @@+import asyncio+from os import getenv+from e2b import Session++E2B_API_KEY = getenv("E2B_API_KEY")+++def print_stdout(output):+ print(output.line)++async def main():+ session = await Session.create(id="Python3", api_key=E2B_API_KEY)++ # Start a server process in the background+ # We are not using `await background_server` - that would wait for the process to finish running+ background_server = await session.process.start( # $HighlightLine+ "python3 -m http.server 8000", # $HighlightLine+ on_stdout=print_stdout, # $HighlightLine+ ) # $HighlightLine++ # Wait for the server to be accessible+ await asyncio.sleep(1)++ # Start another process that creates request to server+ server_request = await session.process.start("curl localhost:8000")++ # Wait for the server request to finish running+ request_output = await server_request.finished++ # Stop the background process (it would otherwise run indefinitely). After this the background_server.output should be populated with stdout and stderr that were outputted until the server was killed+ await background_server.kill() # $HighlightLine++ # Access the server output after the server process is killed+ server_output = background_server.output # $HighlightLine+++ await session.close()+++asyncio.run(main())diff --git a/apps/docs/src/components/Navigation.tsx b/apps/docs/src/components/Navigation.tsxindex 507257042..07a02d807 100644--- a/apps/docs/src/components/Navigation.tsx+++ b/apps/docs/src/components/Navigation.tsx@@ -247,6 +247,7 @@ export const navigation = [{ title: 'SDK Basics', href: '/getting-started/basics' },{ title: 'SDK Timeouts', href: '/getting-started/sdk-timeouts' },{ title: 'SDK Logging', href: '/getting-started/sdk-logging' },+ { title: 'SDK Multiple Processes', href: '/getting-started/sdk-multiple-processes' },],},{diff --git a/package-lock.json b/package-lock.jsonindex 6a21da0ad..c3b855431 100644--- a/package-lock.json+++ b/package-lock.json@@ -10062,7 +10062,7 @@},"packages/js-sdk": {"name": "@e2b/sdk",- "version": "0.2.1",+ "version": "0.2.4","license": "MIT","dependencies": {"cross-fetch": "^3.1.5",@@ -10103,7 +10103,7 @@},"packages/python-sdk": {"name": "@e2b/python-sdk",- "version": "0.2.7"+ "version": "0.2.8"}},"dependencies": {