Parallel tasks

2 snippets across 2 stacks — Bash & Linux, Python

SHBash & Linux

Wait for Background Jobs

SH · Process Management
syntax
wait [pid|%job]
example
process_a &
process_b &
wait
echo 'Both finished'

Note wait with no arguments paits for all background jobs. With a PID or job spec, it waits for just that one. Useful in scripts to parallelize tasks and then synchronize before continuing.

PYPython

asyncio.TaskGroup (3.11+)

PY · Modern Features
syntax
async with asyncio.TaskGroup() as tg:
    tg.create_task(coro())
example
import asyncio

async def fetch(url: str) -> str:
    await asyncio.sleep(0.1)
    return f"Data from {url}"

async def main():
    async with asyncio.TaskGroup() as tg:
        task1 = tg.create_task(fetch("/api/users"))
        task2 = tg.create_task(fetch("/api/orders"))

    print(task1.result())
    print(task2.result())

# asyncio.run(main())
output
Data from /api/users
Data from /api/orders

Note TaskGroup replaces asyncio.gather() with structured concurrency. If any task raises, all other tasks are cancelled and errors are collected into an ExceptionGroup.