国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

Django如何使用asyncio協程和ThreadPoolExecutor多線程

瀏覽:154日期:2024-09-21 10:57:09

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 Viewimport asyncioimport timefrom 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 Viewimport timefrom 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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Django
相關文章:
主站蜘蛛池模板: 国产2021中文天码字幕 | 成人在线免费网站 | 国产日韩欧美swag在线观看 | 国产免费一区二区三区在线观看 | 亚洲综合亚洲 | 久久久久国产一级毛片高清片 | 综合免费视频 | 国内9l视频自拍 | 免费国产不卡午夜福在线观看 | 国产精品久久久久久小说 | 久久欧美久久欧美精品 | 欧美色爱综合 | 日韩一区二区精品久久高清 | 久热中文字幕在线精品免费 | 台湾精品视频在线观看 | 日本免费人做人一区在线观看 | 国产日韩精品一区二区 | 美女把张开腿男生猛戳免费视频 | 亚洲欧美高清视频 | 亚洲美女影院 | 性盈盈影院67194 | 免费视频网站一级人爱视频 | 日本中文字幕不卡免费视频 | 国产不卡视频在线观看 | 特级欧美视频aaaaaa | 中文字幕曰韩一区二区不卡 | 一级爱爱片一级毛片-一毛 一级爱做片免费观看久久 一级白嫩美女毛片免费 | 男人精品一线视频在线观看 | 日韩欧美高清在线 | 免费一级特黄欧美大片勹久久网 | 国产高清精品自在久久 | 亚洲自拍成人 | 精品国产一区在线观看 | 成年人在线网站 | 国产精品久久久久久久久久98 | 欧美性色高清生活片 | 欧美一级色| 极品五月天 | 特黄特黄黄色大片 | 日韩精品亚洲专区在线观看 | 亚洲免费视频网站 |