Python SDK for Inngest
Project description
Serverless event-driven queues, background jobs, and scheduled jobs for Python.
Works with any framework and platform.
Inngest Python SDK
🚧 Currently in alpha! Not guaranteed to be production ready! 🚧
Supported frameworks:
- Fast API
- Flask
- Tornado
Usage
Basic (no steps)
This is a minimal example of an Inngest function. It's missing some of our features but it's a good starting point.
import flask
import inngest.flask
import requests
@inngest.create_function(
fn_id="find_person",
trigger=inngest.TriggerEvent(event="app/person.find"),
)
async def fetch_person(
*,
event: inngest.Event,
step: inngest.Step,
**_kwargs: object,
) -> dict:
person_id = event.data["person_id"]
res = requests.get(f"https://swapi.dev/api/people/{person_id}")
return res.json()
app = flask.Flask(__name__)
inngest_client = inngest.Inngest(app_id="flask_example")
# Register functions with the Inngest server
inngest.flask.serve(
app,
inngest_client,
[fetch_person],
)
app.run(port=8000)
Step run
The following example registers a function that will:
- Get the person ID from the event
- Fetch the person with that ID
- Fetch the person's ships
- Return a summary dict
@inngest.create_function(
fn_id="find_ships",
trigger=inngest.TriggerEvent(event="app/ships.find"),
)
async def fetch_ships(
*,
event: inngest.Event,
step: inngest.Step,
**_kwargs: object,
) -> dict:
"""
Find all the ships a person has.
"""
person_id = event.data["person_id"]
def _fetch_person() -> dict:
res = requests.get(f"https://swapi.dev/api/people/{person_id}")
return res.json()
# Wrap the function with step.run to enable retries
person = step.run("fetch_person", _fetch_person)
def _fetch_ship(url: str) -> dict:
res = requests.get(url)
return res.json()
ship_names = []
for ship_url in person["starships"]:
# step.run works in loops!
ship = step.run("fetch_ship", lambda: _fetch_ship(ship_url))
ship_names.append(ship["name"])
return {
"person_name": person["name"],
"ship_names": ship_names,
}
Avoiding async functions
Completely avoiding `async`` functions only requires 2 differences:
- Use
step: inngest.StepSync
instead ofstep: inngest.Step
- Use
serve_sync
instead ofserve
@inngest.create_function(
fn_id="find_person",
trigger=inngest.TriggerEvent(event="app/person.find"),
)
def fetch_person(
*,
event: inngest.Event,
step: inngest.StepSync,
**_kwargs: object,
) -> dict:
person_id = event.data["person_id"]
res = requests.get(f"https://swapi.dev/api/people/{person_id}")
return res.json()
app = flask.Flask(__name__)
inngest_client = inngest.Inngest(app_id="flask_example")
inngest.flask.serve_sync(
app,
inngest_client,
[fetch_person],
)
💡 You can mix
async
and non-async
functions in the same app!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
inngest-0.1.0a4.tar.gz
(34.7 kB
view hashes)
Built Distribution
inngest-0.1.0a4-py3-none-any.whl
(41.4 kB
view hashes)
Close
Hashes for inngest-0.1.0a4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5a8915b310f473533fff2a289e781c641a063d535c587f3583ebed07f58fbd5d |
|
MD5 | 3dd0220a998b8d8c404dd40f1fe358c0 |
|
BLAKE2b-256 | e1ba92d7d9cf9e6db9b79e84438e9ae242622ed66e96fa09f5060a8019960e81 |