Introduction to Activities
An Activity is a function that executes a single, well-defined action (either short or long running), which handles operations that can fail. Here are some examples:
- Sending e-mails
- API calls
- Database writes
- Network requests
An Activity involves code that is prone to failure because if it fails (let’s say the API is down), Temporal automatically retries it over and over until it succeeds or until your customized retry or timeout configuration is hit.
Here are two Activities: one for withdrawing money and one for depositing money.
from temporalio import activity
@activity.defn
async def withdraw_money(amount: float) -> bool:
# This would usually contain code that is prone to failure,
# like an API call, but it is a print statement here for simplicity.
print(f"Successfully withdrawn ${amount}")
return True
@activity.defn
async def deposit_money(amount: float) -> bool:
print(f"Successfully deposited ${amount}")
return True
See our code in more detail here.