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({})
终端中的错误:
/home/fractaluser/Desktop/Dev/cache/env/lib/python3.5/site-packages/pymongo/topology.py:150: UserWarning: MongoClient opened before fork. Create MongoClient only after forking. See PyMongo's documentation for details: http://api.mongodb.org/python/current/faq.html#is-pymongo-fork-safe
"MongoClient opened before fork. Create MongoClient only "
/home/fractaluser/Desktop/Dev/cache/env/lib/python3.5/site-packages/pymongo/topology.py:150: UserWarning: MongoClient opened before fork. Create MongoClient only after forking. See PyMongo's documentation for details: http://api.mongodb.org/python/current/faq.html#is-pymongo-fork-safe
"MongoClient opened before fork. Create MongoClient only "
在这里,我使用多处理在Python中运行多个任务。我正在使用Pymongo客户端从MongoDB获取数据。
我无法理解这个错误。因此,我也没有得到结果。
请看看
您是否尝试过在processwork2()
和processwork1()
方法中创建Mongo连接?
您要在fork()
系统调用发生之前创建杂种型(可能是由OS创建新过程的一部分(。
只是将此部分放入方法的主体中:
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["game"]
mycol = mydb["test_collection"]