django - 如何在 Django REST 框架中运行异步 curl 请求
问题描述
如何在 Django REST 框架中运行异步 curl 请求。该请求应该在后台运行。
我的代码是这样的:
def track(self, appUserId, appEventName, appEventData):
asyncio.run(self.main(appUserId, appEventName, appEventData))
async def main(self, appUserId, appEventName, appEventData):
async with aiohttp.ClientSession() as session:
tasks = []
task = asyncio.ensure_future(self.sendData(session, {
"userId": self.appUserIdSandbox if (self.sandBoxEnabled == True) else appUserId,
"eventName": appEventName,
"eventData": appEventData
}))
tasks.append(task)
task_count = await asyncio.gather(*tasks)
async def sendData(self,session, appEventData):
response = {}
payload = json.dumps(appEventData)
headers = {
"Authorization": self.__authKey,
"Cache-Control": "no-cache",
"Content-Type": "application/json",
}
async with session.post(self.__apiUrl,
headers=headers,
data=payload) as response:
result_data = await response.json()
return response
解决方案
推荐阅读
- javascript - 尝试使用画布在 img 元素中显示用户选择的视频帧
- c# - 实体框架核心 DBContext 数据模型作为变量
- php - 当我按下提交输入按钮时,如何使用 AJAX 跳过部分 php?
- amazon-redshift - 如何创建日期的纪元列表
- java - java如何处理StackOverflowError?
- php - 通过 AJAX 在 Laravel 中更新刀片
- angular - Understanding Angular 6 services
- angular - 我可以知道单选按钮是否未选择角度吗?
- angular - Angular 5:如何监听组件中的选定选项?
- tcp - MQTT CONNECT 头:如何定义用户名和密码?