Skip to content

Concurrency and resources

Runs are coroutines: an application runs as many at once as its callers start, each in its own sandbox. This page says what bounds them, what they share, and which resources a sandbox can be given.

What bounds the number of runs

Bound Set with Counts
Per application FastSandbox(max_concurrency=4) The runs of every task of the application
Per task @app.task(max_concurrency=2) The runs of one task

A run waits for a slot before it connects, and holds it until its sandbox is removed: the bound counts sandboxes, not only calls of functions. None or 0 means no bound, the default. Running tasks shows them in use.

A bound holds within one event loop

Each bound is an asyncio.Semaphore of the event loop that runs the task. It does not count the runs of another event loop, another thread (several run_sync calls in parallel threads), or another process: several workers of the same application each have their own bound, and nothing bounds them together. The daemon may have limits of its own, which fastsandbox does not know.

No other limit exists in fastsandbox. To bound the sandboxes of several processes, bound the processes.

What runs share

  • The client. An application connects once when it is entered, or once per run otherwise. A client holds no connection: each request opens its own, so runs never wait for each other's requests.
  • The kit cache. The packages of the process are shared by its clients, and a composition is forged by one caller at a time, whatever their event loops, so that concurrent runs of a new kit forge it once.
  • The daemon. Every sandbox of the user, of every process, shares its global network policy, its image store and its resources.
  • The thread pool of the event loop, which runs synchronous tasks and prepares uploads: as many threads as min(32, CPUs + 4) by default.

Each run has its own sandbox, its own network rules and its own keys. Runs given the same host directory through WorkspaceDir() or Mounted share that directory, and agents signed in with one subscription share its account.

Resources of a sandbox

Resource Set with Default
Virtual CPUs cpus=2 on client.create, @app.task, @app.agent, or SandboxSpec.with_cpus(2) The daemon's
Memory memory="4g", in sbx notation, or SandboxSpec.with_memory("4g") The daemon's
Disk Not per sandbox: a setting of the daemon applies to all The daemon's

An upload is built in memory as one archive before it is sent: uploading a large directory takes as much memory on the host. Copy only what the run needs, or mount it.

Capacity

There is no distributed quota. To size a machine, count the sandboxes that can run at once, the sum of the max_concurrency of the processes that use the daemon, times the CPUs and memory each one is given, and leave room for the images the kits pull. client.daemon.disk_usage() reports what the daemon stores.

Next