Run jobs concurrently
Work on Bauplan can be parallelized at two levels:
- Inside a run: independent models and expectations of the same DAG run in parallel automatically, with no action on your side.
- Across jobs: parallelism is up to the caller. Each run, import or query is a separate job, and jobs run at the same time only if you submit them at the same time. All jobs of an organization share the same compute and resources.
Let the DAG parallelize the work inside a run, submit independent jobs from a thread pool of bounded size, and limit concurrency in the orchestrator too.
Let the DAG run independent models in parallel
Start from a properly structured DAG, as described in how to structure a pipeline. Bauplan knows the dependencies between the models and already parallelizes them within the same job when feasible.
Do not split those steps into separate jobs as that increases the complexity of scheduling them and adds fixed costs such as submitting, planning, and reading the inputs of every job.
Run independent jobs from a bounded pool
To run independent pipelines at the same time, submit them from a thread pool whose size is the maximum number of jobs you want running at once. Each worker submits one run detached and waits for it with wait_for_job, a helper that polls Client.get_job until the job finishes.
from concurrent.futures import ThreadPoolExecutor
import bauplan
# The maximum number of runs in flight: start low and raise it while runs keep their usual duration
MAX_CONCURRENT_RUNS = 4
# Independent projects, each writing its own tables
PROJECTS = ["pipelines/orders", "pipelines/inventory", "pipelines/marketing"]
client = bauplan.Client()
def run_and_wait(project_dir: str) -> None:
"""Submit one run detached, and block until it ends"""
state = client.run(project_dir=project_dir, ref=branch, detach=True)
wait_for_job(client, state.job_id, timeout_seconds=3600)
with ThreadPoolExecutor(max_workers=MAX_CONCURRENT_RUNS) as pool:
# A run beyond MAX_CONCURRENT_RUNS starts only when an earlier one has ended
futures = [pool.submit(run_and_wait, project) for project in PROJECTS]
# result() raises the error of a failed run, if any
for future in futures:
future.result()
You can submit many jobs in parallel and the platform schedules them for you, but we recommend not overdoing it, for instance by running one import per file instead of a single import over their prefix; see how to lay out data on object storage. Parallel jobs share the compute available to your organization and, past a certain point, the extra jobs wait for resources instead of finishing the work sooner.
Write each table from one job at a time
Run the jobs that write the same table one after another, and do not run them at the same time. Each job writes on its own transactional branch and merges it at the end, so when two jobs write orders at the same time, the first merge succeeds and the second is rejected, whatever the resources available; see how to handle merge conflicts.
If the jobs are queries, use the same pattern
The same pattern applies to queries, for instance to check the freshness of several tables.
from concurrent.futures import ThreadPoolExecutor
import bauplan
import pyarrow as pa
MAX_CONCURRENT_QUERIES = 8
TABLES = ["analytics.orders", "analytics.inventory", "analytics.campaigns", "analytics.tickets"]
client = bauplan.Client()
def latest_update(table: str) -> pa.Table:
"""Return the most recent update time of a table"""
return client.query(f"SELECT MAX(updated_at) AS latest_update FROM {table}", ref="main")
with ThreadPoolExecutor(max_workers=MAX_CONCURRENT_QUERIES) as pool:
latest_updates = dict(zip(TABLES, pool.map(latest_update, TABLES)))
Share one client across the threads of a pool, as above, but not across processes. If you use multiprocessing, use the spawn start method, and create a client in each process.
Limit and stagger jobs in the orchestrator
The same holds for scheduled pipelines. You can start them all at the same time, but they will then compete for the same compute. Staggering their start times by a few minutes lets each of them finish sooner.