简体   繁体   English

在Python中异步运行多个任务

[英]Run Multiple Tasks Asynchronously in Python

So this is my last 2 lines in one of my endpoint: 所以这是我在端点之一中的最后两行:

self.send_activation_mail(request, user=user)
return self.response(request, status=201, title='Created', description='Please check your email for activation', data=user_data)

returning self.response will be return the my rest client a response of 201. My problem is the send_activation_mail seems like takes time to run so the my endpoint to signup process takes so much time. 返回self.response将返回我的其余客户端201的响应。我的问题是send_activation_mail似乎需要花一些时间才能运行,因此我的注册端点需要花费很多时间。 I tried to find the way to execute those tasks at the same time asynchronously in Python.Does anyone has any experience with this kind of situation before and how do you solve it? 我试图找到一种在Python中同时异步执行这些任务的方法,以前有没有人遇到过这种情况的经验,如何解决呢?

You mean that you want to execute the tasks asynchronously. 您的意思是您要异步执行任务。 Synchronously means executing everything in order, on the same thread. 同步意味着在同一线程上按顺序执行所有操作。

Depending on your version of Python, you can check out the multiprocessing module: https://docs.python.org/2/library/multiprocessing.html . 根据您的Python版本,您可以签出多重处理模块: https : //docs.python.org/2/library/multiprocessing.html

我通过使用线程threading.Thread(target=self.send_activation_mail(request=request, user=user)).start()执行send_activation_mail之后解决了.thread threading.Thread(target=self.send_activation_mail(request=request, user=user)).start()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM