Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions docs/lifespan_tasks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Lifaspan tasks in PyNest

## Introduction

Lifespan tasks - coroutines, which run while app is working.

## Defining a lifespan task
As example of lifespan task will use coroutine, which print time every hour. In real user cases can be everything else.

```python
import asyncio
from datetime import datetime

async def print_current_time():
while True:
current_time = datetime.now().strftime("%H:%M:%S")
print(f"Current time: {current_time}")
await asyncio.sleep(3600)
```

## Implement a lifespan task
In `app_module.py` we can define a startup handler, and run lifespan inside it

```python
from nest.core import PyNestFactory

app = PyNestFactory.create(
AppModule,
description="This is my PyNest app with lifespan task",
title="My App",
version="1.0.0",
debug=True,
)

http_server = app.get_server()

@http_server.on_event("startup")
async def startup():
await print_current_time()
```

Now `print_current_time` will work in lifespan after startup.