zlacker

[parent] [thread] 2 comments
1. simonw+(OP)[view] [source] 2025-05-30 19:55:27
I'm trying this out now and it's very promising. One problem I'm running into with the Python library is that I'd like to keep that sandbox running for several minutes while I do things like set variables in one call and then use them for stuff several calls later. I keep seeing this error intermittently:

    Error: Sandbox is not started. Call start() first
Is there a suggested way of keeping a sandbox around for longer?

The documented code pattern is this:

    async def main():
        async with PythonSandbox.create(name="my-sandbox") as sb:
            exec = await sb.run("print('Hello, World!')")
            print(await exec.output())
Due to the way my code works I want to instantiate the sandbox once for a specific class and then have multiple calls to it by class methods, which isn't a clean fit for that "async with" pattern.

Any recommendations?

replies(2): >>appcyp+ja >>gcharb+ob
2. appcyp+ja[view] [source] 2025-05-30 21:29:51
>>simonw+(OP)
Right. You can skip the `with` context manager and call start and stop yourself.

There is an example of that here:

https://github.com/microsandbox/microsandbox/blob/0c13fc27ab...

3. gcharb+ob[view] [source] 2025-05-30 21:39:01
>>simonw+(OP)
async with is just syntactic sugar. You could very well call __aenter__ and __aexit__ manually. You could also use an AsyncExitStack, call __aenter__ manually, then enter_async_context, and call aclose when you’re done. Since aclose method exists I guess this is not an anti-pattern.

https://docs.python.org/3/library/contextlib.html#contextlib...

[go to top]