-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
45 lines (32 loc) · 886 Bytes
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import asyncio
import logging
import os
import threading
import time
from fastapi import FastAPI
app = FastAPI()
SLEEP_FOR = 10
race_me = None
def log(msg: str):
global race_me
process = os.getpid()
thread = threading.get_native_id()
logging.debug("[%s -> %d/%s] %s", race_me, process, thread, msg)
race_me = f"{process}/{thread}"
log("Defining FastAPI app")
@app.get("/asyncsleep")
async def concurrent_sleep() -> dict:
log("Entering sleep endpoint")
log(f"Starting sleep for {SLEEP_FOR} seconds")
await asyncio.sleep(SLEEP_FOR)
log("Woken up again")
log("Exiting sleep endpoint")
return {}
@app.get("/sleep")
def threadpool_sleep() -> dict:
log("Entering sleep endpoint")
log(f"Starting sleep for {SLEEP_FOR} seconds")
time.sleep(SLEEP_FOR)
log("Woken up again")
log("Exiting sleep endpoint")
return {}