Core concepts
Daemons
A daemon is a process that runs forever. Like scheduled tasks, daemons often play a supporting role in web app backends - for example, by performing some background task such as processing data polled from a website or a queue.
Example
Here, the daemon continuously scrapes a stock price from a website, saving it to a multinode dict. When the service receives a GET
request, the latest stock price is loaded from the dict and returned.
import time
import os
import requests
from fastapi import FastAPI
import uvicorn
import multinode as mn
app = FastAPI()
prices_dict = mn.get_dict(name="prices")
@mn.daemon()
def maintain_up_to_date_stock_price():
while True:
response = requests.get("https://stocksite.com/tickers/ABC")
current_price = extract_price_from_html(response)
prices_dict["ABC"] = current_price
time.sleep(1)
@app.get("/price")
def get_price():
price = prices_dict["ABC"]
return {"price": price}
@mn.service(port=80)
def api():
uvicorn.run(app, host="0.0.0.0", port=80)