# Concurrency control

> **CocoIndex v1.** This page documents CocoIndex **v1** — a ground-up redesign from v0. When writing code, ignore any v0 flow-builder DSL or deprecated decorators.
>
> Source: https://cocoindex.io/docs/advanced_topics/concurrency_control/ · Docs index: https://cocoindex.io/docs/llms.txt · Agent skill: https://cocoindex.io/docs/skill.md
>
> v0→v1 quick map — if you reach for these v0 symbols, stop and use the v1 form: `@cocoindex.flow_def`/`FlowBuilder` → `coco.App` + a `@coco.fn` main function; `add_collector()`/`collect()`/`export()` → declare target states (`declare_row`, `declare_file`); `cocoindex.sources/functions/targets.*` → connector APIs (`localfs.walk_dir`, `coco.ops.*`, `postgres.declare_table_target`). Full mapping + API reference: https://cocoindex.io/docs/skill.md.

CocoIndex executes each [processing component](/docs/programming_guide/processing_component) as a unit of concurrent work. By default, up to **1024** processing components can be in flight per app. When components perform resource-intensive work (e.g., calling external APIs, running ML models), you may want a tighter limit.

## Setting the limit

Set `max_inflight_components` in `AppConfig`:

```python
app = coco.App(
    coco.AppConfig(name="MyPipeline", max_inflight_components=4),
    app_main,
    sourcedir=pathlib.Path("./data"),
)
```

With `max_inflight_components=4`, at most 4 processing components execute at the same time. When a component finishes, the next pending one starts.

Setting `max_inflight_components=1` serializes all components — only one runs at a time.

You can also set the limit via the `COCOINDEX_MAX_INFLIGHT_COMPONENTS` environment variable:

```bash
export COCOINDEX_MAX_INFLIGHT_COMPONENTS=4
```

**Precedence:** `AppConfig` value > environment variable > default (1024).

## Deadlock prevention

When a parent component mounts a child, the parent releases its concurrency slot so the child can make progress. This prevents deadlocks in nested mount scenarios — even with `max_inflight_components=1`, a parent mounting a child will not block forever.
