Python Forum
How to properly close db connection, best practice
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to properly close db connection, best practice
#1
Hi everyone,
I have a module that connects to a db to check for new entries, and if new, trigger more functions etc.

My question is regarding the connection itself. As it currently stands, I create and close the connection in each function, each time a function needs to interact with the db. I'm wondering how this affects performance and would think it better to connect and close just once when the loops is starting up.

My issue is that the module that connects runs a loop, and I don't want to create incremental numbers of connections... at the same time, I don't want to make the connection once and just have it time out or something.

What's the best practice for this?

Here's what some of my code looks like:

def beginSending():
    global last_sent
    cnxn = pyodbc.connect('UID='+dbUser+';PWD='+dbPassword+';DSN='+dbHost)
    cursor = cnxn.cursor()
    cursor.execute(pullId)
    results = cursor.fetchone()
    last_sent = results
    cnxn.close()
    Logger.writeAndPrintLine('Send listener started.', 0)
    sendLoop()

def sendLoop():
    while 0 == 0:
        sendListener()
        time.sleep(2.0)

def sendListener():
    global last_sent
    cnxn = pyodbc.connect('UID='+dbUser+';PWD='+dbPassword+';DSN='+dbHost)
    cursor = cnxn.cursor()
    cursor.execute(pullId)
    results = cursor.fetchone()
    if results != last_sent:
        sendSMS()
    else:
        cnxn.close()
Reply
#2
I've decided to create another loop for the db connection that refreshes the connection and stops all other functions every 10 mins. My next question is how to run multiple functions in parallel so that I can do a sleep() or time.wait() while other stuff runs.
Reply
#3
You'll need the threading or multiprocessing modules. I'm just learning them myself for some Tkinter GUI apps. This is a good, pretty extensive intro to threading.
https://realpython.com/intro-to-python-threading/
"So, brave knights, if you do doubt your courage or your strength, come no further, for death awaits you all with nasty, big, pointy teeth!" - Tim the Enchanter
Reply
#4
(Jan-21-2020, 09:53 PM)Marbelous Wrote: You'll need the threading or multiprocessing modules. I'm just learning them myself for some Tkinter GUI apps. This is a good, pretty extensive intro to threading.
https://realpython.com/intro-to-python-threading/

Haha thanks for the reply. I realized this after doing some digging online... looking into multiprocessing, as that seems more efficient for what I'm trying to do.
Reply
#5
Quote:I'm just learning them myself for some Tkinter GUI apps
With a good design you should not have to use threading in a Tkinter GUI.
Reply
#6
You should also avoid using global variables. Also, line 13: you do realise that Python (like most languages) has a True constant (and a False one, obviously)?
Reply
#7
(Jan-23-2020, 08:03 PM)woooee Wrote:
Quote:I'm just learning them myself for some Tkinter GUI apps
With a good design you should not have to use threading in a Tkinter GUI.
I'm actually finishing the code without bothering with it for now but it's a data logging app that reads several pieces of hardware for each of 16 test sockets through GPIB communications and it takes about 3 seconds for all the data to be collected. It doesn't really matter that the GUI goes non-responsive and of course I could break the task into slices that would allow Tkinter to update the UI more frequently but I want to learn threading, multi-processing and how python's GIL works anyway so I figured now is as good a time as any...
"So, brave knights, if you do doubt your courage or your strength, come no further, for death awaits you all with nasty, big, pointy teeth!" - Tim the Enchanter
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Best practice on using virtual environments in Python bytecrunch 6 806 Feb-14-2024, 03:22 PM
Last Post: snippsat
  Serial connection connection issue Joni_Engr 15 8,026 Aug-30-2021, 04:46 PM
Last Post: deanhystad
  best practice for import libraries and using pyinstaller aster 3 2,862 Apr-17-2021, 11:12 AM
Last Post: snippsat
  Threading best practice EvanS1 2 1,930 Apr-21-2020, 10:11 PM
Last Post: EvanS1
  Help with string practice Hermann_Fegelein 2 2,688 Aug-15-2018, 04:56 PM
Last Post: Hermann_Fegelein
  Best Practice For String Quotations ? Zork_3 9 49,939 Sep-01-2017, 07:16 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020