import pymongo
from gridfs import GridFS


def initialize_mongo_db( config, settings ):
    if ( 'mongodb.use' in settings ) and ( settings['mongodb.use'] == 'true' ):
        conn = pymongo.Connection( settings['mongodb.uri'] )
        config.registry.settings['!mongodb.conn'] = conn
        config.add_subscriber(add_mongo_db, 'pyramid.events.NewRequest')


def add_mongo_db(event):
    settings = event.request.registry.settings
    db = settings['!mongodb.conn'][settings['mongodb.name']]
    event.request.mongodb = db
    event.request.gridfs = GridFS(db)

import pymongo
from gridfs import GridFS


def initialize_mongo_db(config, settings):
    if ('mongodb.use' in settings) and (settings['mongodb.use'] == 'true'):
        conn = pymongo.Connection(settings['mongodb.uri'])
        config.registry.settings['!mongodb.conn'] = conn
        config.set_request_property(add_mongo_db, 'mongodb', reify=True)


def add_mongo_db(request):
    settings = request.registry.settings
    db = settings['!mongodb.conn'][settings['mongodb.name']]
    request.mongodb = db
    request.gridfs = GridFS(db)

Use set_request_property instead of subscriber to improve performance
Use set_request_property instead of subscriber to improve performance

  import pymongo
  from gridfs import GridFS
  
  
- def initialize_mongo_db( config, settings ):
+ def initialize_mongo_db(config, settings):
-     if ( 'mongodb.use' in settings ) and ( settings['mongodb.use'] == 'true' ):
+     if ('mongodb.use' in settings) and (settings['mongodb.use'] == 'true'):
-         conn = pymongo.Connection( settings['mongodb.uri'] )
+         conn = pymongo.Connection(settings['mongodb.uri'])
          config.registry.settings['!mongodb.conn'] = conn
-         config.add_subscriber(add_mongo_db, 'pyramid.events.NewRequest')
+         config.set_request_property(add_mongo_db, 'mongodb', reify=True)
  
  
- def add_mongo_db(event):
+ def add_mongo_db(request):
-     settings = event.request.registry.settings
+     settings = request.registry.settings
      db = settings['!mongodb.conn'][settings['mongodb.name']]
-     event.request.mongodb = db
+     request.mongodb = db
-     event.request.gridfs = GridFS(db)
+     request.gridfs = GridFS(db)
  
Use set_request_property instead of subscriber to improve performance
## Code Before:
import pymongo
from gridfs import GridFS


def initialize_mongo_db( config, settings ):
    if ( 'mongodb.use' in settings ) and ( settings['mongodb.use'] == 'true' ):
        conn = pymongo.Connection( settings['mongodb.uri'] )
        config.registry.settings['!mongodb.conn'] = conn
        config.add_subscriber(add_mongo_db, 'pyramid.events.NewRequest')


def add_mongo_db(event):
    settings = event.request.registry.settings
    db = settings['!mongodb.conn'][settings['mongodb.name']]
    event.request.mongodb = db
    event.request.gridfs = GridFS(db)

## Instruction:
Use set_request_property instead of subscriber to improve performance
## Code After:
import pymongo
from gridfs import GridFS


def initialize_mongo_db(config, settings):
    if ('mongodb.use' in settings) and (settings['mongodb.use'] == 'true'):
        conn = pymongo.Connection(settings['mongodb.uri'])
        config.registry.settings['!mongodb.conn'] = conn
        config.set_request_property(add_mongo_db, 'mongodb', reify=True)


def add_mongo_db(request):
    settings = request.registry.settings
    db = settings['!mongodb.conn'][settings['mongodb.name']]
    request.mongodb = db
    request.gridfs = GridFS(db)
