Django如何使用asyncio協(xié)程和ThreadPoolExecutor多線程
Django視圖函數(shù)執(zhí)行,不在主線程中,直接loop = asyncio.new_event_loop()
# 不能loop = asyncio.get_event_loop() 會(huì)觸發(fā)RuntimeError: There is no current event loop in thread
因?yàn)閍syncio程序中的每個(gè)線程都有自己的事件循環(huán),但它只會(huì)在主線程中為你自動(dòng)創(chuàng)建一個(gè)事件循環(huán)。所以如果你asyncio.get_event_loop在主線程中調(diào)用一次,它將自動(dòng)創(chuàng)建一個(gè)循環(huán)對(duì)象并將其設(shè)置為默認(rèn)值,但是如果你在一個(gè)子線程中再次調(diào)用它,你會(huì)得到這個(gè)錯(cuò)誤。相反,您需要在線程啟動(dòng)時(shí)顯式創(chuàng)建/設(shè)置事件循環(huán):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
在Django單個(gè)視圖中使用asyncio實(shí)例代碼如下(有多個(gè)IO任務(wù)時(shí))
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關(guān)鍵字(python3.5之前使用yield)實(shí)現(xiàn)協(xié)程
"""
self.id = 5
start_time = time.time()
'''
# 同步執(zhí)行
# 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)) # 兩種寫(xiě)法
# 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單個(gè)視圖中使用ThreadPoolExecutor實(shí)例代碼如下(有多個(gè)IO任務(wù)時(shí))
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
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解Python 數(shù)據(jù)庫(kù) (sqlite3)應(yīng)用
本篇文章主要介紹了Python標(biāo)準(zhǔn)庫(kù)14 數(shù)據(jù)庫(kù) (sqlite3),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧。2016-12-12
python matplotlib庫(kù)繪制散點(diǎn)圖例題解析
這篇文章主要介紹了python matplotlib庫(kù)繪制散點(diǎn)圖例題解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08
Python參數(shù)解析器configparser簡(jiǎn)介
configparser是python自帶的配置參數(shù)解析器,可以用于解析.config文件中的配置參數(shù),ini文件中由sections(節(jié)點(diǎn))-key-value組成,這篇文章主要介紹了Python參數(shù)解析器configparser,需要的朋友可以參考下2022-12-12
Python實(shí)現(xiàn)求最大公約數(shù)及判斷素?cái)?shù)的方法
這篇文章主要介紹了Python實(shí)現(xiàn)求最大公約數(shù)及判斷素?cái)?shù)的方法,涉及Python算數(shù)運(yùn)算的相關(guān)技巧,需要的朋友可以參考下2015-05-05
如何基于Python實(shí)現(xiàn)一個(gè)慶祝國(guó)慶節(jié)的小程序
這篇文章主要介紹了如何基于Python實(shí)現(xiàn)一個(gè)慶祝國(guó)慶節(jié)的小程序,增加了互動(dòng)選擇祝福語(yǔ)、查詢信息、播放背景音樂(lè)及趣味小測(cè)驗(yàn)等功能,使用tkinter增強(qiáng)GUI,提升用戶互動(dòng)體驗(yàn),需要的朋友可以參考下2024-09-09

