-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #267 from phenobarbital/dev
sample background executor (like starlette) for navigator
- Loading branch information
Showing
3 changed files
with
77 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import asyncio | ||
from aiohttp import web | ||
from navigator import Application | ||
from navigator.background import BackgroundTask | ||
from app import Main | ||
|
||
# define a new Application | ||
app = Application(Main, enable_jinja2=True) | ||
|
||
async def send_email(email, message): | ||
# Simulate email sending | ||
await asyncio.sleep(10) | ||
print(f"Email sent to {email} with message: {message}") | ||
|
||
|
||
# Using the Application Context | ||
@app.post('/sample_background') | ||
async def handler(request: web.Request) -> web.Response: | ||
data = await request.json() | ||
email = data.get('email') | ||
message = data.get('message') | ||
|
||
# Create a BackgroundTask | ||
background_task = BackgroundTask(send_email, email, message) | ||
|
||
# Schedule the background task to run | ||
asyncio.create_task(background_task.run()) | ||
|
||
return web.Response(text="Background task scheduled") | ||
|
||
|
||
|
||
|
||
if __name__ == '__main__': | ||
try: | ||
app.run() | ||
except KeyboardInterrupt: | ||
print('EXIT FROM APP =========') |