在多线程应用中使用mongodb的正确方法



我有服务器应用程序与cron任务(在自己的线程),我想插入数据到mongodb数据库,我想避免死锁或其他多线程问题。

我代码:

from multiprocessing.dummy import Pool as ThreadPool
from pymongo import MongoClient
import sched
import time
TIME_INTERVAL = 3
THREAD_NUMBER = 4
s = sched.scheduler(time.time, time.sleep)
pool = ThreadPool(THREAD_NUMBER)
websites = [
    "website1",
    "website2",
    "website3",
    "website4",
]
def insert_to_mongo(result):
    #that is proper way ?
    mongo_client = MongoClient('localhost', 27017)
    dic = mongo_client["cjgs"]["tracks"]
    dic.insert({"Result": result})
def parsing_site(station):
    print "Doing stuff for ", station
    insert_to_mongo("Result for " + station)
def recursion(sc, station):
    parsing_site(station)
    sc.enter(TIME_INTERVAL, 1, recursion, (sc, station,))
def run_cron_task(station):
    s.enter(TIME_INTERVAL, 1, recursion, (s, station,))
    s.run()
pool.map(run_cron_task, websites)

如何使用mongodb在这种情况下?如何使用修饰符和其他语法糖以更python风格的方式编写此代码?

你可以使用装饰器来封装所有线程的东西,像这样:

def your_decorator(fun):
    # your threading stuff with fun
@decorator
def you_fun():
   # etc

但我建议你更愿意看看两阶段提交,这是MongoDB自己推荐的

相关内容

  • 没有找到相关文章

最新更新