def connection_processor(handler): db.connect() try: return handler() finally: if not db.is_closed(): db.close()
def build_all_timelines(): r = redis.StrictRedis(host='localhost', port=6379) database.connect() users = User.select() total = users.count() for index, user in enumerate(users): print("Distributing {}'s timeline. {} of {}".format( user.username, index, total)) #This can be done using a zip function and 2 generators but I have more #access to the db. This way I'm using more memory pairs = ((photo.created_at.timestamp(), photo.id) for photo in user.photos) pairs = [item for pair in pairs for item in pair] for follower in user.followers(): tagName = "{}:hometimeline".format(follower.username) r.zadd(tagName, *pairs) if not database.is_closed(): database.close()
def build_all_timelines(): r = redis.StrictRedis(host=os.environ.get('REDIS_HOST', 'localhost')) database.connect() users = UserProfile.select() total = users.count() for index, user in enumerate(users): print("Distributing {}'s timeline. {} of {}".format( user.username, index, total)) #This can be done using a zip function and 2 generators but I have more #access to the db. This way I'm using more memory pairs = ((photo.id, photo.id) for photo in user.statuses) pairs = [item for pair in pairs for item in pair] for follower in user.followers(): if pairs: tagName = "feed:home:{}".format(follower.id) r.zadd(tagName, *pairs) if pairs: tagName = "feed:home:{}".format(user.id) r.zadd(tagName, *pairs) if not database.is_closed(): database.close()
async def after_response(request, response): try: if not db.is_closed(): db.close() except: error_logger.error()
def process_response(self, req, resp, resource): if not database.is_closed(): database.close()