我有4个基本上构建查询并执行它们的函数。我想让它们使用asyncio同时运行。我的asyncio实现似乎是正确的,因为非MongoDB任务运行时应该如此(例如asyncio.sleep())。以下是代码:使用asyncio运行并发mongoengine查询
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
tasks = [
service.async_get_associate_opportunity_count_by_user(me, criteria),
service.get_new_associate_opportunity_count_by_user(me, criteria),
service.async_get_associate_favorites_count(me, criteria=dict()),
service.get_group_matched_opportunities_count_by_user(me, criteria)
]
available, new, favorites, group_matched = loop.run_until_complete(asyncio.gather(*tasks))
stats['opportunities']['available'] = available
stats['opportunities']['new'] = new
stats['opportunities']['favorites'] = favorites
stats['opportunities']['group_matched'] = group_matched
loop.close()
# functions written in other file
@asyncio.coroutine
def async_get_ass(self, user, criteria=None, **kwargs):
start_time = time.time()
query = **query that gets built from some other functions**
opportunities = Opportunity.objects(query).count()
run_time = time.time() - start_time
print("runtime of available: {}".format(run_time))
yield from asyncio.sleep(2)
return opportunities
@asyncio.coroutine
def get_new_associate_opportunity_count_by_user(self, user, criteria=None, **kwargs):
start_time = time.time()
query = **query that gets built from some other functions**
opportunities = Opportunity.objects(query).count()
run_time = time.time() - start_time
print("runtime of new: {}".format(run_time))
yield from asyncio.sleep(2)
return opportunities
@asyncio.coroutine
def async_get_associate_favorites_count(self, user, criteria={}, **kwargs):
start_time = time.time()
query = **query that gets built from some other functions**
favorites = Opportunity.objects(query).count()
run_time = time.time() - start_time
print("runtime of favorites: {}".format(run_time))
yield from asyncio.sleep(2)
return favorites
@asyncio.coroutine
def get_group_matched_opportunities_count_by_user(self, user, criteria=None, **kwargs):
start_time = time.time()
query = **query that gets built from some other functions**
opportunities = Opportunity.objects(query).count()
run_time = time.time() - start_time
print("runtime of group matched: {}".format(run_time))
yield from asyncio.sleep(2)
return opportunities
yield from asyncio.sleep(2)
只是表明函数是异步运行的。这里是在终端上的输出:
组的运行时间相匹配:喜好0.11431598663330078 运行时:0.0029871463775634766 时间戳功能的运行时间:新0.0004897117614746094 运行时:0.13006806373596191 总运行时间:可用0.15225648880004883 运行时2403.2700061798096 根据我的理解,除了由于睡眠功能而增加到总运行时间的2000ms之外,由于所有功能中的最大运行时间都是该值,所以它不应该超过155-160ms。
我目前正在研究motorengine(mongoengine 0.9.0的一个端口),它显然可以启用异步mongodb查询,但我认为我将无法使用它,因为我的模型已经使用mongoengine定义。有没有解决这个问题的方法?