gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 12:47:57 25 4
gpt4 key购买 nike

我有带 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

但我建议您还是看一下 Two Phase Commit , 这是 MongoDB 自己推荐的。

关于python - 在多线程应用程序中使用 mongodb 的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31338199/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com