色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術(shù)文章
文章詳情頁

Django如何使用asyncio協(xié)程和ThreadPoolExecutor多線程

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

Django視圖函數(shù)執(zhí)行,不在主線程中,直接loop = asyncio.new_event_loop() # 不能loop = asyncio.get_event_loop() 會觸發(fā)RuntimeError: There is no current event loop in thread

因為asyncio程序中的每個線程都有自己的事件循環(huán),但它只會在主線程中為你自動創(chuàng)建一個事件循環(huán)。所以如果你asyncio.get_event_loop在主線程中調(diào)用一次,它將自動創(chuàng)建一個循環(huán)對象并將其設(shè)置為默認值,但是如果你在一個子線程中再次調(diào)用它,你會得到這個錯誤。相反,您需要在線程啟動時顯式創(chuàng)建/設(shè)置事件循環(huán):

loop = asyncio.new_event_loop()asyncio.set_event_loop(loop)

在Django單個視圖中使用asyncio實例代碼如下(有多個IO任務(wù)時)

from django.views import Viewimport asyncioimport timefrom django.http import JsonResponse class TestAsyncioView(View): def get(self, request, *args, **kwargs): ''' 利用asyncio和async await關(guān)鍵字(python3.5之前使用yield)實現(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)) # 兩種寫法 # 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任務(wù)時)

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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標簽: Django
相關(guān)文章:
主站蜘蛛池模板: 97久草| 国产91一区二这在线播放 | 国产初高中生粉嫩无套第一次 | 国产午夜精品理论片久久影视 | 92av在线| 免费成人一级片 | 亚洲成人免费在线视频 | 色成人亚洲 | 性色a v 一区 | 日本护士一级毛片在线播放 | 免费乱码中文字幕网站 | 成人免费在线播放视频 | 国产久草视频 | 欧美日韩精品国产一区在线 | 毛片网站在线看 | 欧美亚洲免费久久久 | 91成人在线免费视频 | 91久久国产成人免费观看资源 | 成人免费手机在线看网站 | 国产日韩亚洲欧美 | 日韩免费观看的一级毛片 | 香蕉网影院在线观看免费 | 中文字幕在线观看不卡视频 | 久久精品视频3 | 一级欧美一级日韩片 | 色青五月天 | 亚洲va精品中文字幕动漫 | 最新毛片久热97免费精品视频 | 欧美理论片在线观看一区二区 | 娇喘嗯嗯~轻点啊视频福利 | 国产成人成人一区二区 | 韩国美女爽快一毛片免费 | 白嫩美女直冒白浆 | 国产成人精品女人不卡在线 | 黄色美女在线观看 | 亚洲欧美日韩精品久久亚洲区色播 | 国产精品人成 | 国产三级免费观看 | 国产成人亚洲欧美三区综合 | 国产成人禁片免费观看 | 国产九九视频在线观看 |