def main(): for job in schedule.jobs: project_logger.info(job) while True: schedule.run_pending() time.sleep(10)
def update_weights_in_mongo(): project_logger.info('schedule run: undate_weights_in_mongo') for i in MONGO_DATABASE_PLACES.find({}): shop_id = i['shop_id'] MONGO_DATABASE_PLACES.update({'shop_id': shop_id}, {'$set': {'weights': random.random() * 100}})
# -*- coding: utf-8 -*- import pymongo from config import CONFIG from logger import project_logger client = pymongo.MongoClient(CONFIG.MONGO.HOST, CONFIG.MONGO.PORT) project_logger.info('Connect Mongo Server Success...') db = client.shop_address places = db.places places.create_index([('local', pymongo.GEOSPHERE)]) MONGO_DATABASE_PLACES = places
from config import CONFIG from logger import project_logger mysql_config = { 'host': CONFIG.MYSQL.HOST, 'port': CONFIG.MYSQL.PORT, 'user': CONFIG.MYSQL.USER, 'passwd': CONFIG.MYSQL.PASSWORD, 'db': CONFIG.MYSQL.DATABASE } pool = DBUtils.PooledDB.PooledDB(creator=MySQLdb, mincached=1, maxcached=100, use_unicode=False, charset='utf8', cursorclass=MySQLdb.cursors.DictCursor, **mysql_config) project_logger.info('Connect Mysql Server Success...') MYSQL_CONNECTION = lambda: pool.connection() def fetchall(sql, params=None): conn = MYSQL_CONNECTION() cursor = conn.cursor() try: count = cursor.execute(sql, params) if params else cursor.execute(sql) result = cursor.fetchall() if count else [] except MySQLdb.Error as e: raise e finally: cursor.close() conn.close()