Implementing cache using mysql.connector. Shows Type Error

The Code

First of all hereโ€™s my code:

# importing mysql Connector .... nothing unusual
import mysql.connector as connection

# This function connects to an external database
def dbconn():
    try:
        return connection.connect(** st.secrets["mysql"])
        st.info("DB connected")
    except:
        return None
        st.info("DB NOT connected")
        
# This Variable stores the connection
DB_DATA = dbconn()

# Applying Cache
# The following function takes the DB_DATA variable in and does a lot of stuff with it
# For Example: Make Queries, printing to webpage, lots of pandas calculation
@st.cache(suppress_st_warning=True, hash_funcs={connection.network.MySQLTCPSocket: dbconn})
def my_func(mydb):
...<Code goes here>

NOTE: my_func is meant to render a whole display page, not just to perform queries and calculations

The Error

Now that we have the code out of the way, here is the error I am getting:

Here is the explaination:

My aim is to make queries inside my_func() while also rendering outputs and graphs to the screen. This function is meant to serve as a whole webpage(there are also other functions like this).
But as you might have guessed this process is extremely SLOW, therefore I want to apply @st.cache to it.

I have a feeling that I am doing something very stupid here since the error is a simple Type Error.

Any suggesstion on how do I fix it?
Also, did I provide sufficient info? Iโ€™ll provide more if need be

@st.cache(allow_output_mutation=True, hash_funcs={"_thread.RLock": lambda _: None, "builtins.weakref":lambda _: None}, suppress_st_warning=True)
def loaddb():
    DB_DATA = dbconn()
    return  DB_DATA

DB_DATA = loaddb()

I fixed this problem using the above code.
the function loaddb() is just an intermediary function to assign the DB connection to a variable. We could have done it this way:

DB_DATA = dbconn()

but in order to cache the stuff we needed to convert this particular process in the form of a function.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.