Django如何使用asyncio协程和ThreadPoolExecutor多线程
作者:傻白甜++ 时间:2022-06-22 11:43:34
Django视图函数执行,不在主线程中,直接loop = asyncio.new_event_loop()
# 不能loop = asyncio.get_event_loop() 会触发RuntimeError: There is no current event loop in thread
因为asyncio程序中的每个线程都有自己的事件循环,但它只会在主线程中为你自动创建一个事件循环。所以如果你asyncio.get_event_loop在主线程中调用一次,它将自动创建一个循环对象并将其设置为默认值,但是如果你在一个子线程中再次调用它,你会得到这个错误。相反,您需要在线程启动时显式创建/设置事件循环:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
在Django单个视图中使用asyncio实例代码如下(有多个IO任务时)
from django.views import View
import asyncio
import time
from django.http import JsonResponse
class TestAsyncioView(View):
def get(self, request, *args, **kwargs):
"""
利用asyncio和async await关键字(python3.5之前使用yield)实现协程
"""
self.id = 5
start_time = time.time()
'''
# 同步执行
# results = [self.io_task1(self.id),
# self.io_task2(self.id),
# self.io_task2(self.id)]
'''
loop = asyncio.new_event_loop() # 或 loop = asyncio.SelectorEventLoop()
asyncio.set_event_loop(loop)
self.loop = loop
works = [
asyncio.ensure_future(self.io_task3(5)),
asyncio.ensure_future(self.io_task3(5)),
asyncio.ensure_future(self.io_task3(5)),
asyncio.ensure_future(self.io_task3(5)),
asyncio.ensure_future(self.io_task3(5)),
]
try:
results = loop.run_until_complete(asyncio.gather(*works)) # 两种写法
# results = loop.run_until_complete(self.gather_tasks())
finally:
loop.close()
end_time = time.time()
return JsonResponse({'results': results, 'cost_time': (end_time - start_time)})
async def gather_tasks(self):
tasks = (
self.make_future(self.io_task1, self.id),
self.make_future(self.io_task2, self.id),
self.make_future(self.io_task2, self.id),
self.make_future(self.io_task1, self.id),
self.make_future(self.io_task2, self.id),
self.make_future(self.io_task2, self.id),
)
results = await asyncio.gather(*tasks)
return results
async def make_future(self, func, *args):
future = self.loop.run_in_executor(None, func, *args)
response = await future
return response
def io_task1(self, sleep_time):
time.sleep(sleep_time)
return 66
def io_task2(self, sleep_time):
time.sleep(sleep_time)
return 77
async def io_task3(self, sleep_time):
# await asyncio.sleep(sleep_time)
s = await self.do(sleep_time)
return s
async def do(self, sleep_time):
await asyncio.sleep(sleep_time)
return 66
在Django单个视图中使用ThreadPoolExecutor实例代码如下(有多个IO任务时)
from django.views import View
import time
from concurrent.futures import ThreadPoolExecutor, as_completed
class TestThreadView(View):
def get(self, request, *args, **kargs):
start_time = time.time()
future_set = set()
tasks = (self.io_task1, self.io_task2, self.io_task2, self.io_task1, self.io_task2, self.io_task2)
with ThreadPoolExecutor(len(tasks)) as executor:
for task in tasks:
future = executor.submit(task, 5)
future_set.add(future)
for future in as_completed(future_set):
error = future.exception()
if error is not None:
raise error
results = self.get_results(future_set)
end_time = time.time()
return JsonResponse({'results': results, 'cost_time': (end_time - start_time)})
def get_results(self, future_set):
results = []
for future in future_set:
results.append(future.result())
return results
def io_task1(self, sleep_time):
time.sleep(sleep_time)
return 66
def io_task2(self, sleep_time):
time.sleep(sleep_time)
return 77
来源:https://www.cnblogs.com/feifeifeisir/p/13723054.html
标签:Django,asyncio,协程,ThreadPoolExecutor,多线程
0
投稿
猜你喜欢
Go标准库日志打印及同时输出到控制台与文件
2024-04-26 17:22:40
pytorch 如何把图像数据集进行划分成train,test和val
2023-12-26 15:28:10
Python 实现定积分与二重定积分的操作
2023-08-05 18:22:27
influx+grafana自定义python采集数据和一些坑的总结
2022-08-23 14:55:54
Python 内置函数complex详解
2022-02-16 03:57:41
全面了解python字符串和字典
2021-05-03 21:06:43
详解使用navicat连接远程linux mysql数据库出现10061未知故障
2024-01-24 22:12:44
详解php实现页面静态化原理
2023-10-25 10:58:10
Python中的字典合并与列表合并技巧
2021-01-02 06:07:35
查看端口并杀进程python脚本代码
2022-06-26 21:58:32
Oracle过程与函数的区别分析
2024-01-15 20:15:37
python语言的优势是什么
2022-04-15 18:45:25
matplotlib grid()设置网格线外观的实现
2021-08-26 08:39:13
python实现双链表
2022-06-20 01:47:48
对于Python装饰器使用的一些建议
2022-05-26 09:05:43
详解Git 的 rebase 命令使用方法
2023-04-16 16:57:08
Go语言基础for循环语句的用法及示例详解
2024-02-07 16:15:34
Python中的Pandas 时间函数 time 、datetime 模块和时间处理基础讲解
2022-08-01 05:18:41
MySQL 字符串模式匹配 扩展正则表达式模式匹配
2024-01-20 14:47:41
Oracle数据库处理多媒体信息
2010-07-16 13:01:00