Jakub Novák e48574f69c
Improve file watcher in Python Sync SDK (#457)
# Description

Current solution didn't work well with in sync Python. If you're unsure
whether a file has been created, you might end up waiting for a timeout
to exit the loop.

New implementation uses polling, you can ask for events whenever you
want and it will request envd to send all events (or only new ones).
This should make it much better for users with sync Python SDK

Example of a problematic usage before and after:
## Before
```python
sbx = Sandbox()
watcher = sbx.files.watch("/home/user")
sbx.files.make_dir("test")
for event in watcher:
    print(event)
     # !!! if you don't exit, you would be stuck for the rest of the timeout (default 60 seconds)
    break
watcher.close()
```
## After
```python
sbx = Sandbox()
watcher = sbx.files.watch("/home/user")
sbx.files.make_dir("test")
events = watcher.get_new_events()
watcher.stop()
for event in events:
    print(event)
```

Even worse case was if you don't know if anything will happen:
## Before
```python
sbx = Sandbox()
watcher = sbx.files.watch("/home/user")
if random.random() > 0.5:
    sbx.files.make_dir("test")
# There's 50% chance you will get stuck. The only workaround is to run in in separate thread and you kill it after a while (you aren't really sure when it's safe)
for event in watcher:
    print(event)
    #  !!! if you don't exit, you would be stuck for the rest of the timeout (default 60 seconds)
    break
watcher.close()
```
## After
```python
sbx = Sandbox()
watcher = sbx.files.watch("/home/user")
if random.random() > 0.5:
    sbx.files.make_dir("test")
events = watcher.get_new_events()
watcher.stop()
for event in events:
    print(event)
```
2024-10-14 23:09:53 +02:00
..
2024-09-25 17:17:08 -07:00
2024-10-14 14:01:34 -07:00
2024-08-06 15:47:03 +02:00
2024-07-12 12:22:57 -07:00
2024-09-26 06:48:22 +00:00
2024-10-12 20:49:33 -07:00

e2b logo

E2B SDK

The E2B SDK is made to control the E2B Sandboxes - secure cloud environments for running LLM-generated code. The SDK lets you give your AI app a custom code interpreter.

  • ✔️ Works with any LLM and AI framework (see Cookbook for examples)
  • ✔️ Supports streaming content like charts and stdout, stderr
  • ✔️ Python & JS SDK
  • ✔️ Runs on serverless and edge functions
  • ✔️ Runs AI-generated code in secure sandboxed environments
  • ✔️ 100% open source (including infrastructure)
💻 Supported language runtimes
  • ✔️ Python
  • (Beta) JavaScript, R, Java

Last 1 month downloads for the Python SDK Last 1 month downloads for the Python SDK


What is E2B?

E2B is an open-source runtime for running AI-generated code in secure cloud Sandboxes. It's tailor-made for agentic & AI use cases.

E2B Sandbox

E2B Sandbox is a secure cloud environment that allows AI agents and apps. You can run multiple instances of Sandboxes, and have long-running sessions. Inside the Sandboxes, LLMs can use the same tools as humans do, e.g.:

  • Running LLM generated code
  • Cloud browsers
  • GitHub repositories and CLIs
  • Coding tools like linters, autocomplete, "go-to defintion"
  • Audio & video editing

Getting Started & Documentation

Please visit documentation to get started.

To create and control a sandbox, you use our SDK:

Install SDK

pip install e2b

Start sandbox

from e2b import Sandbox

# Create sandbox
sandbox = Sandbox()

# Let an LLM use the sandbox here
# Visit https://e2b.dev/docs/sandbox/overview to learn more about sandboxes.

# Close sandbox once done
sandbox.close()