Question

I have lot of functions with various parameters, inside I am doing something ike this

session = Session()
try:
    # do something with parameters
except Exception as e:
    print e.message
    session.rollback()
finally:
    session.close()

I some functions it looks like it break code/error and do not pass through finally and connection with sqlalchemy/postgresql stays in IDLE forever. For every pass of function I want to create file if doesn't exist with filename 'log_db_' + str(os.getpid()) and write function name inside file and if pass through finally to delete file otherwise stay created. (if it breaks I will see file otherwise not) Is this possible to do with decorator/how or to add in every function this code to handle file ?

Was it helpful?

Solution

Why not just using Python's standard tools ? Like proper exception handling and using the logging module ?

import logging
logger = logging.getLogger(__name__)


def func1(*params):
    logger.debug("BEGIN func1(%s)", (params,))
    session = Session()

    try:
        do_something_with(params)

    except Exception as e:
        logger.exception("FAILED func1(%s)", (params,))

        logger.debug("ROLLINGBACK func1(%s)", (params,))
        session.rollback()
        logger.debug("ROLLEDBACK func1(%s)", (params,))

        # if you don't handle exceptions let them propagate, thanks
        raise 

    else:
        logger.debug("SUCCEEDED func1(%s)", (params,))
        # don't you need a session.commit() somewhere ???

    finally:
        logger.debug("CLOSING func1(%s)", (params,))
        session.close()
        logger.debug("CLOSED func1(%s)", (params,))

Now you just have to configure your logger and you'll probably find out what's going on much more quickly than you'll need to implement an half-baked ad-hoc solution.

Oh and if you have the very same pattern in more than a couple functions with only the params and "do_something_with(params) part differing, then wrap all the logging and transaction handling code in a decorator:

def manage(func):
    def wrapper(*args, **kw):
        session = Session()
        try:
            logger.debug("BEGIN %s(%s, %s)", (func, args,kw))
            result = func(*args, **kw)
            logger.debug("SUCCEEDED %s(%s, %s)", (func, args,kw))
            # don't you need a session.commit() somewhere ???
            return result

        except Exception as e:
            logger.exception("FAILED %s(%s, %s)", (func, args,kw))

            logger.debug("ROLLINGBACK %s(%s, %s)", (func, args,kw))
            session.rollback()
            logger.debug("ROLLEDBACK %s(%s, %s)", (func, args,kw))

            # if you don't handle exceptions let them propagate, thanks
            raise 


        finally:
            logger.debug("CLOSING %s(%s, %s)", (func, args,kw))
            session.close()
            logger.debug("CLOSED %s(%s, %s)", (func, args,kw))

    return wrapper    

    @manage
    def func1(*params):
        do_something_with(params)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top