Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9242679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:37:49+00:00 2026-06-18T08:37:49+00:00

I have had just discovered a really big problem with my Pyramid application. The

  • 0

I have had just discovered a really big problem with my Pyramid application. The mongo scaffold implied that event handlers should be used for handling database connections as well. The idea is, when new request comes in, connect to the database, when page is rendered disconnect.
So this is what I did, in my __init.py__

def connectDatabase(event):
    mongo = MongoDB()
    con = mongo.connectDatabase()
    db = con[Cfg_MongoDB_Database]
    event.request.con = con
    event.request.db = db
    redis = Redis()
    con = redis.connectDatabase()
    event.request.redis = con
    log.debug('newrequest')

def closeConnection(event):
    mongo = MongoDB()
    mongo.closeConnection(event.request.con) 
    log.debug('newresponse')   

def main(global_config, **settings):
    config = Configurator(settings=settings, root_factory=Dashboard)
    authentication = AuthTktAuthenticationPolicy(Cfg_Auth_Key, hashalg='sha512',\
        include_ip=False, timeout=3600*24*7, max_age=3600*24*7, reissue_time=3600)
    authorization = ACLAuthorizationPolicy()
    config.set_authentication_policy(authentication)
    config.set_authorization_policy(authorization)
    config.add_static_view('includes', 'includes', cache_max_age=3600)
    config.add_renderer(".html", "pyramid.mako_templating.renderer_factory")  
    config.add_route('dash', '/')
    config.add_subscriber(connectDatabase, NewRequest)
    config.add_subscriber(closeConnection, NewResponse)
    log.debug('main')
    config.scan()
    return config.make_wsgi_app()

However I’ve put some logger into method that is connecting to the database, and discovered the method is being called 10+ times, and if I disable caching in add_static_view over 100+ times. This implies that for each css, image, js, the new connection is made. Which is huge overhead!!! The site takes 300ms to connect to database 100 times!

/Users/jan/Documents/Test2/test2/data.py changed; reloading...
-------------------- Restarting --------------------
2013-02-01 18:32:48,351 DEBUG [test2.tools][MainThread] main
Starting server in PID 12374.
serving on http://127.0.0.1:6543
2013-02-01 18:34:14,451 DEBUG [test2.tools][Dummy-2] newrequest
2013-02-01 18:34:14,582 DEBUG [test2.tools][Dummy-2] newresponse
2013-02-01 18:34:14,952 DEBUG [test2.tools][Dummy-3] newrequest
2013-02-01 18:34:14,953 DEBUG [test2.tools][Dummy-3] newresponse
2013-02-01 18:34:17,459 DEBUG [test2.tools][Dummy-4] newrequest
2013-02-01 18:34:17,474 DEBUG [test2.tools][Dummy-4] newresponse
2013-02-01 18:34:17,482 DEBUG [test2.tools][Dummy-5] newrequest
2013-02-01 18:34:17,497 DEBUG [test2.tools][Dummy-5] newresponse
2013-02-01 18:34:19,158 DEBUG [test2.tools][Dummy-2] newrequest
2013-02-01 18:34:19,159 DEBUG [test2.tools][Dummy-2] newresponse

So the solution so far is to move the connection to Model instead. But how would I know than when can I close connection? Anyone have any better, more elegant solution for my problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:37:50+00:00Added an answer on June 18, 2026 at 8:37 am

    pymongo has built in connection pooling http://api.mongodb.org/python/current/api/pymongo/mongo_client.html#pymongo.mongo_client.MongoClient

    Using connection pooling shouldn’t create a new connection on every request

    Also, I don’t think your pyramid app should be serving static media in production for exactly this reason, apache,nginx are optimized to serve files, and your requests won’t have to go through your app

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just have read sass changelog and found out that FSSM (the gem that had
I just discovered this problem today, and I had no idea what caused this
I have actually discovered my problem but I am really want to know why
I have just had a brain block, I have a Deck object and want
I have a project hosted with git. I just recently had to reinstall ubuntu,
I have just migrated to mysql 5.5.20 and I had a performance issue with
I had assume I could just do this, but I don't have a way
I have had a persistent problem with Visual Studio 2010 and any framework version
I have had this problem crop up a few times and I can't figure
I just had some weird behavior from a version of g++ for Windows that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.