Print logs on the console multiple times

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.

I don’t see any duplicates. That all looks as expected.

Maybe my question is not clerly enough

simplify the question why these logs print twice?

[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]

actually it print the same times equal to the workers I have set. Is these normal?

and My server have 16 cpu cores. when run with fast=True. The logs printed 16 times!!!

I hope I have described the problem clearly

Yes, once per worker. You can see the pid in the log entry

that means every worker will create a instance of Sanic Extensions:?
TBH,the log print is never mind.
but when i attache db to app

def create_app() -> Sanic:
    app = Sanic(__name__) # web
    setup_db(app)
    return app

it also create many instance , what i want is to keep only one db pool instance in my programer.
is there any solution?

Thanks for replaying.
I have see your answer in discord.
But I still have lots of question.

Still see the example before.
That means every worker will init a Sanic Extension?

Waiting for answer…
Is there any examples to attach db connections to app.ctx?

yes. Each app instance is its own memory location. Therefore they each have an instance of extensions, routes, and everything. This is how multiprocessing works.

Since you are Y using Extensions, I’d suggest using something similar to this pattern here:

Regarding multiple instance issues, you shouldn’t really be so concerned. If you want multiple workers, this is how to do it regardless of the framework you are using. Multiple workers cannot share memory location (*mostly true)