Skip to content

Commit b1845c9

Browse files
committed
[Python] FastAPI: Using a common module app.py for socketify tests
1 parent 3e83cbc commit b1845c9

5 files changed

Lines changed: 60 additions & 53 deletions

File tree

frameworks/Python/fastapi/app-socketify-asgi.py

Lines changed: 0 additions & 47 deletions
This file was deleted.

frameworks/Python/fastapi/app.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import multiprocessing
22
from contextlib import asynccontextmanager
33

4-
import asyncpg
4+
try:
5+
import asyncpg
6+
_DB_ALLOWED = True
7+
except ImportError:
8+
_DB_ALLOWED = False
9+
510
import os
611
from fastapi import FastAPI, Request
712
from fastapi.responses import PlainTextResponse
@@ -34,8 +39,6 @@ def get_num_queries(queries):
3439
return query_count
3540

3641

37-
connection_pool = None
38-
3942
templates = Jinja2Templates(directory="templates")
4043

4144

@@ -60,7 +63,7 @@ async def lifespan(app: FastAPI):
6063
await app.state.connection_pool.close()
6164

6265

63-
app = FastAPI(lifespan=lifespan)
66+
app = FastAPI(lifespan=lifespan) if _DB_ALLOWED else FastAPI()
6467

6568

6669
@app.get("/json")
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env python
2+
import os
3+
import sys
4+
import fastapi
5+
6+
from app import app
7+
8+
if __name__ == "__main__":
9+
import optparse
10+
import multiprocessing
11+
import logging
12+
13+
parser = optparse.OptionParser("usage: %prog [options]", add_help_option=False)
14+
parser.add_option("-h", "--host", dest="host", default='0.0.0.0', type="string")
15+
parser.add_option("-p", "--port", dest="port", default=8080, type="int")
16+
parser.add_option("-s", "--server", dest="server", default="", type="string")
17+
parser.add_option("-w", "--workers", dest="workers", default=0, type="int")
18+
parser.add_option("-k", "--keepalive", dest="keepalive", default=60, type="int")
19+
parser.add_option("-v", "--verbose", dest="verbose", default=0, type="int")
20+
(opt, args) = parser.parse_args()
21+
22+
_is_travis = os.environ.get('TRAVIS') == 'true'
23+
24+
workers = opt.workers
25+
if workers <= 0:
26+
workers = int(multiprocessing.cpu_count())
27+
28+
if _is_travis:
29+
workers = 2
30+
31+
def run_app():
32+
if opt.server in [ 'si', 'socketify' ]:
33+
import socketify
34+
siapp = socketify.ASGI(app)
35+
siapp.listen(opt.port, lambda config: logging.info(f"Listening on port http://localhost:{opt.port} now\n"))
36+
siapp.run()
37+
return
38+
39+
raise Exception(f'Unknown server name = "{opt.server}"')
40+
41+
def create_fork():
42+
n = os.fork()
43+
# n greater than 0 means parent process
44+
if not n > 0:
45+
run_app()
46+
47+
# fork limiting the cpu count - 1
48+
for i in range(1, workers):
49+
create_fork()
50+
51+
run_app() # run app on the main process too :)

frameworks/Python/fastapi/fastapi-socketify-asgi-pypy.dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ RUN pip3 install -r requirements-socketify-pypy.txt
1313

1414
EXPOSE 8080
1515

16-
CMD python ./app-socketify-asgi.py
16+
CMD python app_server.py -s socketify

frameworks/Python/fastapi/fastapi-socketify-asgi.dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ EXPOSE 8080
1616

1717
ENV USE_ORJSON=1
1818

19-
CMD python ./app-socketify-asgi.py
19+
CMD python app_server.py -s socketify

0 commit comments

Comments
 (0)