I use a python script (docker container) to write to Redis db (docker container). The script main objective is to write to Redis db. But there are also other scripts that write to the same Redis db. So where should i make the connection to redis db inside a function in script or globally ?
If your python project is long running (e.g. a web app or a daemon script that runs forever) and making repeated calls, open a single connection and reuse it.
If your python code is short lived script (e.g. it runs for a few seconds then exits) then it doesn't matter so much. Even then, if it's making multiple reads/writes it's better to open one connection and reuse it in the script.
By the wording of your question, it sounds as though you might be thinking of opening the connection outside the script? I'm not really sure where you're going with that, so I can't answer there.
Is it a good practice to open connection to Redis database inside the function that writes to it or outside globally?