Quick Start

This quick start assumes you have intermediate programming skills and are familiar with python, asyncio and Docker.

Cowait quick start

  1. Install cowait
pip install cowait
  1. Pull the base Cowait image. Don't worry - you can use your own Dockerfile if you want to.
docker pull cowait/task
  1. Create a new Cowait task, hello.py:
hello.py
import asyncio
from cowait import task

@task
async def Hello():
    print("Hello World")
  1. Run your Cowait task, this spins up a new docker container.
cowait run hello
  1. Start the Cowait UI
cowait agent

You can visit the UI at http://localhost:1339

  1. If you run your task again, it should show up in the UI.

Asyncio, Inputs & Outputs

  1. Create a new file sleep.py.

Cowait tasks are defined with the async keyword. This allows us to wait for other tasks in an asynchronous fashion, or to use basic features from asyncio, like sleep(n).

sleep.py
import asyncio
from cowait import task

@task
async def Sleep():
    for i in range(5):
      await asyncio.sleep(1)
      print("slept", i + 1)
  1. Modify the Sleep task to take duration as an input. Also return how long it slept.

    • Inputs that you do not define explicitly in the function signature are passed in **inputs.
    • Outputs can be consumed by other tasks or systems.
sleep.py
import asyncio
from cowait import task

@task
async def Sleep(duration: int = 5, **inputs):
    for i in range(duration):
        await asyncio.sleep(1)
        print("slept", i + 1)

    return {
        "duration": duration,
    }
  1. The Cowait CLI allows you to pass inputs when running your task:
cowait run sleep --input duration=7

Parallel Tasks

One of the core features of Cowait is its simple interface to paralellize work on multiple containers. Let's add a new task that spawns multiple Sleep tasks in parallel:

parallel.py
import asyncio
from cowait import task, join
from sleep import Sleep

@task
async def Parallel():
    tasks = [Sleep(duration=5), Sleep(duration=5)]

    result = await join(tasks)

    return result
cowait run parallel

Nice! Here's an illustration of what you just ran, in terms of containers:

Parallel Docker Illustration

You will note that the program doesn't run for precisely 5 seconds, and that the Sleep containers may start / exit at different times (however parallel will block until both are done). This is because there is some overhead in the underlying docker engine to create and spawn new containers for the tasks.

Found incorrect information, typos or have other suggestions to this documentation page?

Edit on Github