2016-09-19 18 views
0

在当前场景中result2不会被执行,直到result1完成其执行。我想执行select * from Test1select * from Test2异步.result2不应该等待结果1完成它的执行。最后同时发送两个结果集给客户端。如何在django中异步执行postgre查询

def fetchQueryData(request): 
     cur=connection.cursor() 

     cur.execute("select * from Test1") 
     result1=cur.fetchall() 
     json1=result1[0][0] 

     cur.execute("select * from Test2") 
     result2=cur.fetchall() 
     json2=result2[0][0] 

     cur.close() 

     return JsonResponse([json1,json2]) 
+1

此[博客文章](http://techspot.zzzeek.org/2015/02/15/asynchronous-python-and-databases/)可能对您有所帮助 – dahrens

+0

django频道 –

回答

2

Celery可以帮助你。您可以启动异步任务并等待它们在while循环中完成。它需要一些设置和服务器上的附加软件。

对我来说,您的查询似乎很长时间运行。我不认为在单个请求中等待他们是关于用户体验的最佳解决方案,但这可能是另一个话题。