Quick Start
This quick start assumes you have intermediate programming skills and are familiar with python, asyncio and Docker.
Cowait quick start
- Install cowait
pip install cowait
- Pull the base Cowait image. Don't worry - you can use your own Dockerfile if you want to.
docker pull cowait/task
- Create a new Cowait task,
hello.py
:
import asyncio
from cowait import task
@task
async def Hello():
print("Hello World")
- Run your Cowait task, this spins up a new docker container.
cowait run hello
- Start the Cowait UI
cowait agent
You can visit the UI at http://localhost:1339
- If you run your task again, it should show up in the UI.
Asyncio, Inputs & Outputs
- 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)
.
import asyncio
from cowait import task
@task
async def Sleep():
for i in range(5):
await asyncio.sleep(1)
print("slept", i + 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.
- Inputs that you do not define explicitly in the function signature are passed in
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,
}
- 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:
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:
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.