Hi. There.
I have a question on running Sanic with multiple times
code is below
#server.py
from sanic import Sanic
from sanic.response import text
app = Sanic("MyHelloWorldApp")
@app.get("/")
async def hello_world(request):
return text("Hello, world.")
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000, workers=2, access_log=True,debug=True,auto_reload=True)
python server.py
output is below
[2023-04-02 21:11:39 +0800] [2116747] [INFO]
┌────────────────────────────────────────────────────────────────────────────────────┐
│ Sanic v23.3.0 │
│ Goin' Fast @ http://0.0.0.0:8000 │
├───────────────────────┬────────────────────────────────────────────────────────────┤
│ │ mode: debug, w/ 2 workers │
│ ▄███ █████ ██ │ server: sanic, HTTP/1.1 │
│ ██ │ python: 3.10.6 │
│ ▀███████ ███▄ │ platform: Linux-5.15.0-69-generic-x86_64-with-glibc2.35 │
│ ██ │ auto-reload: enabled │
│ ████ ████████▀ │ packages: sanic-routing==22.8.0, sanic-ext==23.3.0 │
│ │ │
│ Build Fast. Run Fast. │ │
└───────────────────────┴────────────────────────────────────────────────────────────┘
[2023-04-02 21:11:39 +0800] [2116747] [DEBUG] Creating multiprocessing context using 'spawn'
[2023-04-02 21:11:39 +0800] [2116747] [DEBUG] Starting a process: Sanic-Server-0-0
[2023-04-02 21:11:39 +0800] [2116747] [DEBUG] Starting a process: Sanic-Server-1-0
[2023-04-02 21:11:39 +0800] [2116747] [DEBUG] Starting a process: Sanic-Reloader-0
[2023-04-02 21:11:39 +0800] [2116759] [INFO] Sanic Extensions:
[2023-04-02 21:11:39 +0800] [2116759] [INFO] > injection [0 dependencies; 0 constants]
[2023-04-02 21:11:39 +0800] [2116759] [INFO] > openapi [http://0.0.0.0:8000/docs]
[2023-04-02 21:11:39 +0800] [2116759] [INFO] > http
[2023-04-02 21:11:39 +0800] [2116759] [INFO] > templating [jinja2==3.1.2]
[2023-04-02 21:11:39 +0800] [2116758] [INFO] Sanic Extensions:
[2023-04-02 21:11:39 +0800] [2116758] [INFO] > injection [0 dependencies; 0 constants]
[2023-04-02 21:11:39 +0800] [2116758] [INFO] > openapi [http://0.0.0.0:8000/docs]
[2023-04-02 21:11:39 +0800] [2116758] [INFO] > http
[2023-04-02 21:11:39 +0800] [2116758] [INFO] > templating [jinja2==3.1.2]
[2023-04-02 21:11:39 +0800] [2116759] [DEBUG] Process ack: Sanic-Server-1-0 [2116759]
[2023-04-02 21:11:39 +0800] [2116758] [DEBUG] Process ack: Sanic-Server-0-0 [2116758]
[2023-04-02 21:11:39 +0800] [2116759] [INFO] Starting worker [2116759]
[2023-04-02 21:11:39 +0800] [2116758] [INFO] Starting worker [2116758]
I’m confused why these logs are output multiple times. In fact, the number of outputs is equal to the number of workers. When I initialized the database, it was also initialized many times too. Is this a bug?
How can I initialize once with multiple workers.
Waiting for your reply . Thanks.