MongoClient在派生之前打开。仅创建MongoClient

时间:2019-05-01 18:58:16

标签: python mongodb multiprocessing

while number = int(1,20):
    print ("What grocery would you like to purchase?")

终端错误:

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["game"]
mycol = mydb["test_collection"]


class ProcessWork:
    res1 = []
    res2 = []

    def processwork1(request, res1):
        country = request.GET.get('country')
        qry = {'country': country}
        query = mycol.find(qry).limit(10)
        result = json.loads(dumps(query))
        res1.append(result)

    def processwork2(request, res2):
        country = request.GET.get('country')
        qry = {'country': country}
        query = mycol.find(qry).skip(10).limit(10)
        result = json.loads(dumps(query))
        res2.append(result)

    def process(request):
        t1 = multiprocessing.Process(target=ProcessWork.processwork1, args=((request), ProcessWork.res1))
        t2 = multiprocessing.Process(target=ProcessWork.processwork1, args=((request), ProcessWork.res2))

        t1.start()
        t1.join()
        t2.start()
        t2.join()
        # result = ProcessWork.res1[0] + ProcessWork.res2[0]
        return JsonResponse({})

在这里,我正在使用多重处理在python中运行多个任务。 我正在使用pymongo客户端从mongodb获取数据。

我无法理解这个错误。 由于这个结果,我也没有得到结果。

请看看

1 个答案:

答案 0 :(得分:0)

您是否尝试过在processwork2()processwork1()方法中创建Mongo连接?

您正在fork()系统调用发生之前创建MongoClient(可能是OS创建新进程的一部分)。

只需将此部分放在方法主体中即可

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["game"]
mycol = mydb["test_collection"]