Bagi4 commited on
Commit
ad5c97c
1 Parent(s): 01eda67

Upload 2 files

Browse files
Files changed (2) hide show
  1. main.py +29 -26
  2. requirements.txt +19 -0
main.py CHANGED
@@ -1,29 +1,32 @@
1
- import logging
2
- import uvicorn
3
- from fastapi import FastAPI
4
- from pydantic import BaseModel
5
- import os
6
-
7
- logging.basicConfig(
8
- format='%(asctime)s.%(msecs)03d %(levelname)-8s %(message)s',
9
- level=logging.DEBUG,
10
- datefmt='%Y-%m-%d %H:%M:%S'
11
- )
12
-
13
 
14
  app = FastAPI()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
- requests = []
17
-
18
- @app.post("/sign", tags=["Classificator"])
19
- async def sign(data: str):
20
- requests.append(data)
21
- return result
22
-
23
- @app.get("/ping", tags=["TEST"])
24
- async def ping():
25
- return "pong"
26
-
27
-
28
- if __name__ == "__main__":
29
- uvicorn.run(app, host="127.0.0.1", port=8000)
 
1
+ from fastapi import FastAPI, WebSocket
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  app = FastAPI()
4
+ worker_ws = None
5
+ client_ws = None
6
+
7
+
8
+ @app.websocket("/ws")
9
+ async def websocket_endpoint(websocket: WebSocket):
10
+ global client_ws
11
+ await websocket.accept()
12
+ client_ws = websocket
13
+ while True:
14
+ data = await websocket.receive_text()
15
+ if type(worker_ws) is WebSocket:
16
+ await worker_ws.send_json(data)
17
+ else:
18
+ await websocket.send_text(f"Worker is sleeping now!")
19
+
20
+
21
+ @app.websocket("/worker/ws")
22
+ async def websocket_endpoint(websocket: WebSocket):
23
+ global worker_ws
24
+ await websocket.accept()
25
+ worker_ws = websocket
26
+ while True:
27
+ data = await websocket.receive_json()
28
+ if type(client_ws) is WebSocket:
29
+ await client_ws.send_json(data)
30
+ else:
31
+ await websocket.send_text(f"Client is sleeping now!")
32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
requirements.txt ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ annotated-types==0.6.0
2
+ anyio==4.3.0
3
+ click==8.1.7
4
+ exceptiongroup==1.2.0
5
+ fastapi==0.110.0
6
+ h11==0.14.0
7
+ httptools==0.6.1
8
+ idna==3.6
9
+ pydantic==2.6.4
10
+ pydantic_core==2.16.3
11
+ python-dotenv==1.0.1
12
+ PyYAML==6.0.1
13
+ sniffio==1.3.1
14
+ starlette==0.36.3
15
+ typing_extensions==4.10.0
16
+ uvicorn==0.29.0
17
+ uvloop==0.19.0
18
+ watchfiles==0.21.0
19
+ websockets==12.0