【发布时间】:2015-09-29 01:13:43
【问题描述】:
我有带有 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 风格的方式编写这段代码?
【问题讨论】:
标签: python multithreading mongodb python-2.7 python-multithreading