-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
40 lines (31 loc) · 947 Bytes
/
server.py
File metadata and controls
40 lines (31 loc) · 947 Bytes
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
import logging
import sys
import time
from api.utils import func_invoker
# config
FUNCTIONS = {
"add": lambda x, y: x + y,
"sleep": lambda x: time.sleep(x) or x,
}
# FastAPI
if len(sys.argv) > 0 and sys.argv[0].split("/")[-1] == "uvicorn":
import fastapi
SERVER = func_invoker.FastapiServer(functions=FUNCTIONS, loginfo=logging.getLogger("uvicorn").info)
# uvicorn server:app
app = fastapi.FastAPI(
title="Functions API",
description="author: [TongZJ](https://github.com/Instinct323)\n\n" + SERVER.func_docs(),
version="1.0.0"
)
@app.post("/invoke")
async def invoke(request):
return await SERVER.get_response(request)
# Zmq
else:
import zmq
from api.utils.zmq_socket import ZmqSocket
SERVER = func_invoker.ZmqSocketServer(
functions=FUNCTIONS, loginfo=print,
sock=ZmqSocket(zmq.REP, addr="/tmp/myzmq", queue_size=10)
)
SERVER.main()