块线程执行直到在测试python django中完成
我有一个Django应用程序,需要进行一些3D服务调用以计算嵌入。这个呼叫需要时间并阻止执行,我想使其异步。为此,我正在使用线程。
class MyView():
def create_answer_embedding(answer):
embedding = long_computing_call(answer)
db.write(embedding) <- pseudo code, the idea is that after creating the embedding, we create an AnswerEmbedding obj. in the db
def post(request, *args, **kwargs):
answer = request.data.get("answer")
Thread(target=self.create_answer_embedding, args=(answer, )).start()
return Response()
因此,线程开始计算嵌入,视图返回响应。计算完成后,将在DB中创建一个新的回答对象。
现在我需要测试它正常工作。因此,如果我只是运行一个进行API调用的测试:
response = client.post("/my-view")
answer_embedding = AnswerEmbedding.objects.get(answer__id=response.json()['id'])
此测试会失败,因为它在实际创建线程中创建之前检查了Answerembedding.objects.get
。
因此,我需要以某种方式使此线程运行同步或找到正确模拟它的方法。我只能模拟该线程,但是这样,测试将无法检查是否创建了嵌入。
有什么想法吗? 谢谢
I have a Django app that needs to make some 3d service call to compute embedding. This call takes time and blocking execution and I want to make it async. For this I am using Thread.
class MyView():
def create_answer_embedding(answer):
embedding = long_computing_call(answer)
db.write(embedding) <- pseudo code, the idea is that after creating the embedding, we create an AnswerEmbedding obj. in the db
def post(request, *args, **kwargs):
answer = request.data.get("answer")
Thread(target=self.create_answer_embedding, args=(answer, )).start()
return Response()
So the thread starts calculating embedding and the view returns a response. Once calculating is finished, a new AnswerEmbedding object is created in the DB.
Now I need to test it works properly. So if I just run a test that makes an api call:
response = client.post("/my-view")
answer_embedding = AnswerEmbedding.objects.get(answer__id=response.json()['id'])
This test fails because it checks AnswerEmbedding.objects.get
before it is actually created in the Thread.
So, I need to somehow either make this Thread running sync or find a way to properly mock it. I could just mock the Thread but with this the test will not check if the embedding is created or not.
Any ideas?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想知道,使用线程有什么意义?
如果您仍然想阻止它,我认为您可以使用
join()
,或者您的问题在等待
嵌入
要创建的I'm wondering, What's the point of using thread?
if you still want to block it, I think you could use
join()
or if your problem is waiting the
embedding
to be created you can use state checker