Share
Embed
Django 4.1のAsynchronous
talk about "Asynchronous ORM interface, Asynchronous handlers for class-based views".
Nov 12, 2022
DjangoCongress JP 2022 at NIKKEI Conference Room Nikkei Building
https://djangocongress.jp/
Junya Fukuda
November 12, 2022
Transcript
151 slides
-
•ా ൏ʢJunya Fukudaʣʢ@JunyaFffʣ •גࣜձࣾຊγεςϜٕݚʢJSLʣॴଐ ݝͷձࣾ •WebΤϯδχΞ •GEEKLAB.NAGANO - ίϛϡχςΟεϖʔεӡӦ •ʢ͓͠͝ͱͰʣόϦϡʔϒοΫε
•ຊ͖ʹѪ͞ΕΔαʔϏεΛࢦͯ͠ʢݹຊങऔɾൢചʣ •GEEKLAB.NAGANO - ίϛϡχςΟεϖʔεӡӦ -
Python実践 •20221݄ൃച ٕज़ධࣾ •Python 3 ΤϯδχΞೝఆ࣮ફࢼݧ ओڭࡐ •2ʹ͓͢͢Ίʂࣙॻతʹ͍ͬͯ͋͛ͯͩ͘͞ʂ • ҎԼͷষΛ୲͍ͯ͠·͢
• Chapter2 ίʔσΟϯάن • Chapter5 ܕώϯτ • Chapter19 ฒߦॲཧɼฒྻॲཧ -
Django 4.1 Asynchronous IUUQTEPDTEKBOHPQSPKFDUDPNFOSFMFBTFT Note that, at this stage, the
underlying database operations remain synchronous -
Django 4.1 Asynchronous ͜ͷஈ֊Ͱɺجຊతͳσʔλϕʔεૢ࡞ಉظͷ··Ͱ͋Δ͜ͱʹҙ͍ͯͩ͘͠͞ɻ IUUQTEPDTEKBOHPQSPKFDUDPNFOSFMFBTFT Note that, at this stage,
the underlying database operations remain synchronous -
( 話 ?) •ڈͷDjangoCongress JP 2021 ͰͷτʔΫΛΞοϓσʔτ͍ͨ͠ •Django 3.2 ASGI
ରԠ - ͜Θ͘ͳ͍ asyncio جૅͱ async view ͷ͍ॴ IUUQTMPHNJKQUFDIBSUJDMFT •DjangoͷORMͳͥਖ਼ࣜରԠͰͳ͍ͷ͔ •͔ͨ͠͠Βͦ͏ࢥΘΕͨํ͍Βͬ͠ΌΔ͔͠Ε·ͤΜʢΘͨ͠ -
•Django 4.1 ඇಉظରԠͱ͍··Ͱͷ͓͞Β͍ •ΫϥεϕʔεϏϡʔͷඇಉظରԠ •؆୯ͳίʔυ ΫϥεϕʔεϏϡʔͷଆ •ಉظɾඇಉظΫϥεϕʔεϏϡʔͰͷಈ࡞ൺֱ •ඇಉظ ORM ΠϯλʔϑΣʔε
•֓ཁ •ಉظɾඇಉظORMͰͷಈ࡞ൺֱ •ඇಉظORM ΠϯλʔϑΣʔεͷ͜͏ଆ -
•Django 4.1 ඇಉظରԠͱ͍··Ͱͷ͓͞Β͍ •ΫϥεϕʔεϏϡʔͷඇಉظରԠ •؆୯ͳίʔυ ΫϥεϕʔεϏϡʔͷଆ •ಉظɾඇಉظΫϥεϕʔεϏϡʔͰͷಈ࡞ൺֱ •ඇಉظ ORM ΠϯλʔϑΣʔε
•֓ཁ •ಉظɾඇಉظORMͰͷಈ࡞ൺֱ •ඇಉظORM ΠϯλʔϑΣʔεͷ͜͏ଆ -
- Django ⾮同期対応 2005 Django OSS 1.0 1.4 LTS 2012
2008 •Έͳ͞Μ͕ॳΊͯ͞ΘͬͨDjangoͷόʔδϣϯ͍ͭ͘Ͱ͠ΐ͏͔ʁ -
- Django ⾮同期対応 2005 Django OSS 1.0 1.4 LTS 1.8
LTS 2015 2012 2008 •Έͳ͞Μ͕ॳΊͯ͞ΘͬͨDjangoͷόʔδϣϯ͍ͭ͘Ͱ͠ΐ͏͔ʁ -
- Django ⾮同期対応 2005 Django OSS 1.0 1.4 LTS 1.8
LTS 1.11 LTS 2.0 2017 2015 2012 2008 •Έͳ͞Μ͕ॳΊͯ͞ΘͬͨDjangoͷόʔδϣϯ͍ͭ͘Ͱ͠ΐ͏͔ʁ -
- Django ⾮同期対応 2005 Django OSS 1.0 1.4 LTS 1.8
LTS 1.11 LTS 2.0 2.2 LTS 3.0 2019 2017 2015 2012 2008 •Έͳ͞Μ͕ॳΊͯ͞ΘͬͨDjangoͷόʔδϣϯ͍ͭ͘Ͱ͠ΐ͏͔ʁ -
- Django ⾮同期対応 2005 Django OSS 1.0 1.4 LTS 1.8
LTS 1.11 LTS 2.0 2.2 LTS 3.0 3.2 LTS 4.0 2021 2019 2017 2015 2012 2008 •Έͳ͞Μ͕ॳΊͯ͞ΘͬͨDjangoͷόʔδϣϯ͍ͭ͘Ͱ͠ΐ͏͔ʁ -
- Django ⾮同期対応 2005 Django OSS 1.0 1.4 LTS 1.8
LTS 1.11 LTS 2.0 2.2 LTS 3.0 3.2 LTS 4.0 4.1 2022 2021 2019 2017 2015 2012 2008 •Έͳ͞Μ͕ॳΊͯ͞ΘͬͨDjangoͷόʔδϣϯ͍ͭ͘Ͱ͠ΐ͏͔ʁ -
- Django ⾮同期対応 2005 Django OSS 1.0 1.4 LTS 1.8
LTS 1.11 LTS 2.0 2.2 LTS 3.0 3.2 LTS 4.0 4.1 4.2 LTS 2023 2022 2021 2019 2017 2015 2012 2008 •Έͳ͞Μ͕ॳΊͯ͞ΘͬͨDjangoͷόʔδϣϯ͍ͭ͘Ͱ͠ΐ͏͔ʁ -
- Django ⾮同期対応 1.4 LTS 1.8 LTS 1.11 LTS 2.0
3.0 3.2 LTS 4.1 4.2 LTS 2023 2022 2021 2019 2017 2015 2012 2.2 LTS •Έͳ͞Μ͕ॳΊͯ͞ΘͬͨDjangoͷόʔδϣϯ͍ͭ͘Ͱ͠ΐ͏͔ʁ -
- Django ⾮同期対応 1.4 LTS 1.8 LTS 1.11 LTS 2.0
3.0 3.2 LTS 4.1 4.2 LTS 2023 2022 2021 2019 2017 2015 2012 2014 Python 3.4 asyncio 2.2 LTS •Έͳ͞Μ͕ॳΊͯ͞ΘͬͨDjangoͷόʔδϣϯ͍ͭ͘Ͱ͠ΐ͏͔ʁ -
- Django ⾮同期対応 1.4 LTS 1.8 LTS 1.11 LTS 2.0
3.0 3.2 LTS 4.1 4.2 LTS 2023 2022 2021 2019 2017 2015 2012 2014 Python 3.4 asyncio async/await Python 3.5 2.2 LTS •Έͳ͞Μ͕ॳΊͯ͞ΘͬͨDjangoͷόʔδϣϯ͍ͭ͘Ͱ͠ΐ͏͔ʁ -
- Django ⾮同期対応 1.4 LTS 1.8 LTS 1.11 LTS 2.0
3.0 3.2 LTS 4.1 4.2 LTS 2023 2022 2021 2019 2017 2015 2012 2014 Python 3.4 asyncio async/await 2016 Python 3.5 asgiref 1.0 2.2 LTS •Έͳ͞Μ͕ॳΊͯ͞ΘͬͨDjangoͷόʔδϣϯ͍ͭ͘Ͱ͠ΐ͏͔ʁ -
- Django ⾮同期対応 1.4 LTS 1.8 LTS 1.11 LTS 2.0
2.2 LTS 3.0 3.2 LTS 4.1 4.2 LTS 2023 2022 2021 2019 2017 2015 2012 2014 Python 3.4 asyncio async/await asgiref 1.0 2016 Python 3.5 channels •Έͳ͞Μ͕ॳΊͯ͞ΘͬͨDjangoͷόʔδϣϯ͍ͭ͘Ͱ͠ΐ͏͔ʁ -
- Django ⾮同期対応 1.4 LTS 1.8 LTS 1.11 LTS 2.0
2.2 LTS 3.0 3.2 LTS 4.1 4.2 LTS 2023 2022 2021 2019 2017 2015 2012 2014 Python 3.4 asyncio async/await asgiref 1.0 2016 Python 3.5 channels asgiref 3.0 •Έͳ͞Μ͕ॳΊͯ͞ΘͬͨDjangoͷόʔδϣϯ͍ͭ͘Ͱ͠ΐ͏͔ʁ -
- Django ⾮同期対応 1.4 LTS 1.8 LTS 1.11 LTS 2.0
2.2 LTS 3.2 LTS 4.1 4.2 LTS 2023 2022 2021 2019 2017 2015 2012 2014 Python 3.4 asyncio async/await asgiref 1.0 2016 Python 3.5 channels asgiref 3.0 3.0 asgiରԠ •Έͳ͞Μ͕ॳΊͯ͞ΘͬͨDjangoͷόʔδϣϯ͍ͭ͘Ͱ͠ΐ͏͔ʁ -
- Django ⾮同期対応 1.4 LTS 1.8 LTS 1.11 LTS 2.0
2.2 LTS 3.2 LTS 4.1 4.2 LTS 2023 2022 2021 2019 2017 2015 2012 2014 Python 3.4 asyncio async/await asgiref 1.0 2016 Python 3.5 channels asgiref 3.0 3.0 ؔview asgiରԠ •Έͳ͞Μ͕ॳΊͯ͞ΘͬͨDjangoͷόʔδϣϯ͍ͭ͘Ͱ͠ΐ͏͔ʁ -
- Django ⾮同期対応 1.4 LTS 1.8 LTS 1.11 LTS 2.0
2.2 LTS 3.2 LTS 4.1 4.2 LTS 2023 2022 2021 2019 2017 2015 2012 2014 Python 3.4 asyncio async/await asgiref 1.0 2016 Python 3.5 channels asgiref 3.0 3.0 asgiରԠ ؔview ΫϥεϏϡʔ ORM IF •Έͳ͞Μ͕ॳΊͯ͞ΘͬͨDjangoͷόʔδϣϯ͍ͭ͘Ͱ͠ΐ͏͔ʁ -
- Django ⾮同期対応 1.4 LTS 1.8 LTS 1.11 LTS 2.0
2.2 LTS 3.2 LTS 4.1 4.2 LTS 2023 2022 2021 2019 2017 2015 2012 2014 Python 3.4 asyncio async/await asgiref 1.0 2016 Python 3.5 channels asgiref 3.0 3.0 asgiରԠ ؔview ΫϥεϏϡʔ ORM IF •Έͳ͞Μ͕ॳΊͯ͞ΘͬͨDjangoͷόʔδϣϯ͍ͭ͘Ͱ͠ΐ͏͔ʁ -
- Django ⾮同期対応 1.4 LTS 1.8 LTS 1.11 LTS 2.0
2.2 LTS 3.2 LTS 4.1 4.2 LTS 2023 2022 2021 2019 2017 2015 2012 2014 Python 3.4 asyncio async/await asgiref 1.0 2016 Python 3.5 channels asgiref 3.0 3.0 asgiରԠ ؔview ΫϥεϏϡʔ ORM IF DEP 0009: Async-capable DjangoʢDEPDjangoͷPEPʣ •Έͳ͞Μ͕ॳΊͯ͞ΘͬͨDjangoͷόʔδϣϯ͍ͭ͘Ͱ͠ΐ͏͔ʁ -
- asyncio 基本 •ඇಉظॲཧͷίʔυΛॻͨ͘Ίͷඪ४ϥΠϒϥϦ •ຖόʔδϣϯਐԽ ߴϨϕϧAPI͕ॆ࣮ - ͱͯॻ͖͍͢ •ෳͷλεΫΛಉ࣌ʹॲཧ͢Δ •I/OόϯυͳॲཧʢDBΞΫηε/HTTPϦΫΤετͳͲʣͰޮՌΛൃش
•async/await ߏจ •λεΫΛొ͢Δasyncio.create_task() - ʮλεΫʯ = ॲཧͩͱࢥ͍ͬͯͩ͘͞ɻ •ॲཧΛฒߦʹ͢Δasyncio.gather() •ྫ֎ΩϟϯηϧΛॊೈʹ asyncio.TaskGroup •αʔυύʔςΟͷϥΠϒϥϦΛ͏߹ɺඇಉظରԠ͍ͯ͠Δඞཁ͕͋Δ -
Django 63- σΟεύο νϟʔ 63-DPOG Ϗϡʔ ϑΥʔϜ ςϯϓϨʔτ Ϟσϧ DjangoϓϩδΣΫτ
ϒϥβ Webαʔό ΞϓϦέʔγϣϯαʔό ϦΫΤ ετ ֎෦Ϧιʔε ϛυϧ ΣΞ Ϩεϙ ϯε -
Django 63- σΟεύο νϟʔ 63-DPOG Ϗϡʔ ϑΥʔϜ ςϯϓϨʔτ Ϟσϧ DjangoϓϩδΣΫτ
ϒϥβ Webαʔό ΞϓϦέʔγϣϯαʔό ϦΫΤ ετ ֎෦Ϧιʔε ϛυϧ ΣΞ Ϩεϙ ϯε ʮݱͰ͑ΔDjangoͷڭՊॻʯΛࢀߟʹ͍͖ͤͯͨͩ͞·ͨ͠ -
Django 63- σΟεύο νϟʔ 63-DPOG Ϗϡʔ ϑΥʔϜ ςϯϓϨʔτ Ϟσϧ DjangoϓϩδΣΫτ
ϒϥβ Webαʔό ΞϓϦέʔγϣϯαʔό ϦΫΤ ετ ֎෦Ϧιʔε ϛυϧ ΣΞ Ϩεϙ ϯε ඇಉظରԠ ΫϥεϕʔεϏϡʔ 03.ΠϯλʔϑΣʔε -
Django 63- σΟεύο νϟʔ 63-DPOG Ϗϡʔ ϑΥʔϜ ςϯϓϨʔτ Ϟσϧ DjangoϓϩδΣΫτ
ϒϥβ Webαʔό ΞϓϦέʔγϣϯαʔό ϦΫΤ ετ ֎෦Ϧιʔε ϛυϧ ΣΞ Ϩεϙ ϯε ඇಉظରԠ ΫϥεϕʔεϏϡʔ 03.ΠϯλʔϑΣʔε -
Django 63- σΟεύο νϟʔ 63-DPOG Ϗϡʔ ϑΥʔϜ ςϯϓϨʔτ Ϟσϧ DjangoϓϩδΣΫτ
ϒϥβ Webαʔό ΞϓϦέʔγϣϯαʔό ϦΫΤ ετ ֎෦Ϧιʔε ϛυϧ ΣΞ Ϩεϙ ϯε ඇಉظରԠ ΫϥεϕʔεϏϡʔ 03.ΠϯλʔϑΣʔε -
•Django 4.1 ඇಉظରԠͱ͍··Ͱͷ͓͞Β͍ •ΫϥεϕʔεϏϡʔͷඇಉظରԠ •؆୯ͳίʔυ ΫϥεϕʔεϏϡʔͷଆ •ಉظɾඇಉظΫϥεϕʔεϏϡʔͰͷಈ࡞ൺֱ •ඇಉظ ORM ΠϯλʔϑΣʔε
•֓ཁ •ಉظɾඇಉظORMͰͷಈ࡞ൺֱ •ඇಉظORM ΠϯλʔϑΣʔεͷ͜͏ଆ -
ΫϥεϕʔεϏϡʔͷඇಉظରԠ import asyncio from django.http import HttpResponse from django.views import
View class AsyncView(View): views.py -
ΫϥεϕʔεϏϡʔͷඇಉظରԠ import asyncio from django.http import HttpResponse from django.views import
View class AsyncView(View): async def get(self, request, *args, **kwargs): # Perform view logic using await. await asyncio.sleep(1) return HttpResponse("Hello async world!") views.py -
ΫϥεϕʔεϏϡʔͷඇಉظରԠ import asyncio from django.http import HttpResponse from django.views import
View async def post(self, request, *args, **kwargs): ... async def put(self, request, *args, **kwargs): ... class AsyncView(View): async def get(self, request, *args, **kwargs): # Perform view logic using await. await asyncio.sleep(1) return HttpResponse("Hello async world!") views.py -
ΫϥεϕʔεϏϡʔͷඇಉظରԠ import asyncio from django.http import HttpResponse from django.views import
View async def post(self, request, *args, **kwargs): ... class AsyncView(View): async def get(self, request, *args, **kwargs): # Perform view logic using await. await asyncio.sleep(1) return HttpResponse("Hello async world!") views.py async def put(self, request, *args, **kwargs): ... ఆٛ࣌ͷҙ -
ΫϥεϕʔεϏϡʔͷඇಉظରԠ import asyncio from django.http import HttpResponse from django.views import
View async def post(self, request, *args, **kwargs): ... class AsyncView(View): async def get(self, request, *args, **kwargs): # Perform view logic using await. await asyncio.sleep(1) return HttpResponse("Hello async world!") views.py async def put(self, request, *args, **kwargs): ... ఆٛ࣌ͷҙ -
ΫϥεϕʔεϏϡʔͷඇಉظରԠ import asyncio from django.http import HttpResponse from django.views import
View def post(self, request, *args, **kwargs): ... class AsyncView(View): async def get(self, request, *args, **kwargs): # Perform view logic using await. await asyncio.sleep(1) return HttpResponse("Hello async world!") views.py async def put(self, request, *args, **kwargs): ... ఆٛ࣌ͷҙ -
ΫϥεϕʔεϏϡʔͷඇಉظରԠ import asyncio from django.http import HttpResponse from django.views import
View def post(self, request, *args, **kwargs): ... class AsyncView(View): async def get(self, request, *args, **kwargs): # Perform view logic using await. await asyncio.sleep(1) return HttpResponse("Hello async world!") views.py async def put(self, request, *args, **kwargs): ... ఆٛ࣌ͷҙ *NQSPQFSMZ$PO fi HVSFE -
ΫϥεϕʔεϏϡʔͷඇಉظରԠ import asyncio from django.http import HttpResponse from django.views import
View async def post(self, request, *args, **kwargs): ... class AsyncView(View): async def get(self, request, *args, **kwargs): # Perform view logic using await. await asyncio.sleep(1) return HttpResponse("Hello async world!") views.py async def put(self, request, *args, **kwargs): ... ఆٛ࣌ͷҙ -
Django/views/generics/base.py class View: ⾮同期対応 @classproperty def view_is_async(cls): handlers = [
getattr(cls, method) for method in cls.http_method_names if (method != "options" and hasattr(cls, method)) ] if not handlers: return False is_async = asyncio.iscoroutinefunction(handlers[0]) if not all(asyncio.iscoroutinefunction(h) == is_async for h in handlers[1:]): raise ImproperlyConfigured( f"{cls.__qualname__} HTTP handlers must either be all sync or all " "async." ) return is_async -
Django/views/generics/base.py class View: ⾮同期対応 ... if self.view_is_async: async def func():
return response return func() else: return response -
ΫϥεϕʔεϏϡʔಉظίʔυ class PokeView(View): URL = "https://pokeapi.co/api/v2/pokemon/" def get(self, request, *args,
**kwargs): start = time() pokemons = [] for number in range(1, 152): url = f'https://pokeapi.co/api/v2/pokemon/{number}' r = requests.get(url) pokemon = r.json() pokemons.append({"name" : pokemon['name']}) finish = (time() - start) context = {"pokemons": pokemons, "finish_time": finish} return render(request, “sync.html", context) views.py ֎෦APIΛ࣮ߦ -
ΫϥεϕʔεϏϡʔඇಉظίʔυ class AsyncPokeView(View): URL = "https://pokeapi.co/api/v2/pokemon/" async def get(self, request,
*args, **kwargs): start = time() async with httpx.AsyncClient() as client: tasks = [self.access_url_poke(client, number) for number in range(1, 152)] pokemmons = await asyncio.gather(*tasks, return_exceptions=True) finish = (time() - start) context = {"pokemons": pokemmons, "finish_time": finish} return render(request, "async.html", context) async def access_url_poke(self, client, num: int) -> str: r = await client.get(f"{self.URL}{num}") pokemon = r.json() return {"name": pokemon['name']} views.py ֎෦APIΛ࣮ߦ -
同期 ⾮同期 •࣮ߦ݁Ռ •ಉظɿ18ඵ •ඇಉظɿ0.7ඵ •ඇಉظͱ͍ͯ •APIΛ151ճୟ͘ඇಉظʹ༗རͳܭଌʢͻ͍͖ •ͦͷଞͷ͍ํ - DjangoCon
2022 •Async Django: The practical guide you've been *awaiting* for by Carlton Gibson •https://www.youtube.com/watch?v=B5uQPwX4VLo •֎෦APIΛಉ࣌ʹ࣮ߦ͢Δ߹ʹ࠾༻͠ͳ͍खͳ͍ •ʮඇಉظDjangoͷ͋ͳ͕ͨͪΜͰ͍࣮ͨ༻తͳΨΠυϒοΫʯνϟοτ Channels -
•Django 4.1 ඇಉظରԠͱ͍··Ͱͷ͓͞Β͍ •ΫϥεϕʔεϏϡʔͷඇಉظରԠ •؆୯ͳίʔυ ΫϥεϕʔεϏϡʔͷଆ •ಉظɾඇಉظΫϥεϕʔεϏϡʔͰͷಈ࡞ൺֱ •ඇಉظ ORM ΠϯλʔϑΣʔε
•֓ཁ •ಉظɾඇಉظORMͰͷಈ࡞ൺֱ •ඇಉظORM ΠϯλʔϑΣʔεͷ͜͏ଆ -
⾮同期 ORM •QuerySetͷϝιουʹ಄ࣙʮaʯ͕͍ͭͨඇಉظ൛͕Ճ •໋໊نଇ3ͭͷީิ 1."_async" pre fi x, 2."_async" suf
fi x 3."a" pre fi x _async_get get_async aget ଞʹa.getͳͲ •ٞͷɺPythonͷํੑ(e.g. __aiter__, __anext__, __aenter__, __aexit__ etc.) •3͕࠾༻ʹ IUUQTGPSVNEKBOHPQSPKFDUDPNUOBNJOHPGBTZODWBSJBOUT -
⾮同期 ORM •QuerySetͷϝιουʹ಄ࣙʮaʯ͕͍ͭͨඇಉظ൛͕Ճ •db.models.guery •aiterator, aaggregate, acount, aget, acreate, abulk_create,
abulk_update, • aget_or_create, aupdate_or_create, aearliest, alatest, a fi rst, alast, ain_bulk, • aupdate, adelete, aexists, acontains, aexplain •ʢ´-`ʣ.ŇoOʢ໊લ͕ؾʹͳΔͷ͋Δ… acount, aggregate, adelete …ʣ •·ͣ؆୯ʹಈ͔ͯ͠Έ·͠ΐ͏ -
ΫϥεϕʔεϏϡʔඇಉظίʔυ $ python manage.py shell Python 3.11.0 (v3.11.0:deaf509e8f, Oct 24
2022, 14:43:23) [Clang 13.0.0 (clang-1300.0.29.30)] Type 'copyright', 'credits' or 'license' for more information IPython 8.6.0 -- An enhanced Interactive Python. Type '?' for help. views.py ֎෦APIΛ࣮ߦ -
ΫϥεϕʔεϏϡʔඇಉظίʔυ $ python manage.py shell Python 3.11.0 (v3.11.0:deaf509e8f, Oct 24
2022, 14:43:23) [Clang 13.0.0 (clang-1300.0.29.30)] Type 'copyright', 'credits' or 'license' for more information IPython 8.6.0 -- An enhanced Interactive Python. Type '?' for help. views.py ֎෦APIΛ࣮ߦ In [1]: from django.contrib.auth.models import User -
ΫϥεϕʔεϏϡʔඇಉظίʔυ $ python manage.py shell Python 3.11.0 (v3.11.0:deaf509e8f, Oct 24
2022, 14:43:23) [Clang 13.0.0 (clang-1300.0.29.30)] Type 'copyright', 'credits' or 'license' for more information IPython 8.6.0 -- An enhanced Interactive Python. Type '?' for help. views.py ֎෦APIΛ࣮ߦ In [1]: from django.contrib.auth.models import User In [2]: admin_user = User.objects.get(id=1) In [3]: admin_user Out[3]: <User: admin> -
ΫϥεϕʔεϏϡʔඇಉظίʔυ $ python manage.py shell Python 3.11.0 (v3.11.0:deaf509e8f, Oct 24
2022, 14:43:23) [Clang 13.0.0 (clang-1300.0.29.30)] Type 'copyright', 'credits' or 'license' for more information IPython 8.6.0 -- An enhanced Interactive Python. Type '?' for help. views.py ֎෦APIΛ࣮ߦ In [1]: from django.contrib.auth.models import User In [2]: admin_user = User.objects.get(id=1) In [3]: admin_user Out[3]: <User: admin> In [4]: admin_user = User.objects.aget(id=1) -
ΫϥεϕʔεϏϡʔඇಉظίʔυ $ python manage.py shell Python 3.11.0 (v3.11.0:deaf509e8f, Oct 24
2022, 14:43:23) [Clang 13.0.0 (clang-1300.0.29.30)] Type 'copyright', 'credits' or 'license' for more information IPython 8.6.0 -- An enhanced Interactive Python. Type '?' for help. views.py ֎෦APIΛ࣮ߦ In [1]: from django.contrib.auth.models import User In [2]: admin_user = User.objects.get(id=1) In [3]: admin_user Out[3]: <User: admin> In [4]: admin_user = User.objects.aget(id=1) In [5]: admin_user Out[5]: <coroutine object QuerySet.aget at 0x107b36110> -
ΫϥεϕʔεϏϡʔඇಉظίʔυ $ python manage.py shell Python 3.11.0 (v3.11.0:deaf509e8f, Oct 24
2022, 14:43:23) [Clang 13.0.0 (clang-1300.0.29.30)] Type 'copyright', 'credits' or 'license' for more information IPython 8.6.0 -- An enhanced Interactive Python. Type '?' for help. views.py ֎෦APIΛ࣮ߦ In [1]: from django.contrib.auth.models import User In [2]: admin_user = User.objects.get(id=1) In [3]: admin_user Out[3]: <User: admin> In [4]: admin_user = User.objects.aget(id=1) In [6]: admin_user = await User.objects.aget(id=1) In [5]: admin_user Out[5]: <coroutine object QuerySet.aget at 0x107b36110> -
ΫϥεϕʔεϏϡʔඇಉظίʔυ $ python manage.py shell Python 3.11.0 (v3.11.0:deaf509e8f, Oct 24
2022, 14:43:23) [Clang 13.0.0 (clang-1300.0.29.30)] Type 'copyright', 'credits' or 'license' for more information IPython 8.6.0 -- An enhanced Interactive Python. Type '?' for help. views.py ֎෦APIΛ࣮ߦ In [1]: from django.contrib.auth.models import User In [2]: admin_user = User.objects.get(id=1) In [3]: admin_user Out[3]: <User: admin> In [4]: admin_user = User.objects.aget(id=1) In [6]: admin_user = await User.objects.aget(id=1) In [7]: admin_user Out[7]: <User: admin> In [5]: admin_user Out[5]: <coroutine object QuerySet.aget at 0x107b36110> -
動作 ⽐較 - ORM •ಈ࡞ڥ - ϩʔΧϧ •MacBook Proʢ14Πϯνɺ2021ʣ •Apple
M1 Pro ϝϞϦ32gb -
ΫϥεϕʔεϏϡʔಉظίʔυ class SyncOrmView(View): def get(self, request, *args, **kwargs): start =
time() users = [] for _ in range(100_000): users.append(User(name="Taro")) snippets = [] for _ in range(100_000): snippets.append(Snippet(title="PingPong")) User.objects.bulk_create(users) Snippet.objects.bulk_create(snippets) finish = (time() - start) context = {"finish_time": finish} return render(request, "sync.html", context) views.py ORM -
ΫϥεϕʔεϏϡʔಉظίʔυ class AsyncOrmView(View): async def post(self, request, *args, **kwargs): start
= time() users = [] for _ in range(100_000): users.append(User(name="Taro")) snippets = [] for _ in range(100_000): snippets.append(Snippet(title="PingPong")) await asyncio.gather( self.check("user", User, users), self.check("snippet", Snippet, snippets) ) finish = (time() - start) context = {"finish_time": finish} return render(request, "async.html", context) async def check(self, name, model, data): print(f"start {name}") await model.objects.abulk_create(data) print(f"end {name}") views.py ORM -
同期 ⾮同期 •ಈ࡞ͷൺֱ •ಈ࡞͔Βɺฒߦʹ࣮ߦ͞Ε͍ͯΔ •ಈ࡞ͷൺֱ݁ՌมΘΒͳ͍ •ͪΐͬͱORMͷཪଆͷ࣮ΛݟͯΈ·͠ΐ͏ •ಉظɿ 2.6ඵ •ඇಉظɿ 2.6ඵ
•વͷಈ࡞ - ΠϯλʔϑΣʔεͷΈͷରԠ •ͳͥ͜ͷΑ͏ͳಈ͖ʹͳ͍ͬͯΔͷ͔ -
abulk_create async def abulk_create( self, objs, batch_size=None, ignore_conflicts=False, update_conflicts=False, update_fields=None,
unique_fields=None, ): return await sync_to_async(self.bulk_create)( objs=objs, batch_size=batch_size, ignore_conflicts=ignore_conflicts, update_conflicts=update_conflicts, update_fields=update_fields, unique_fields=unique_fields, ) -
abulk_create async def abulk_create( self, objs, batch_size=None, ignore_conflicts=False, update_conflicts=False, update_fields=None,
unique_fields=None, ): return await sync_to_async(self.bulk_create)( objs=objs, batch_size=batch_size, ignore_conflicts=ignore_conflicts, update_conflicts=update_conflicts, update_fields=update_fields, unique_fields=unique_fields, ) -
sync_to_async •ίʔϧόοΫʹࢦఆ͞Εͨಉظؔʢ͜ͷ߹getʣΛඇಉظͬΆ࣮͘ߦͯ͠ฦ͢ def sync_to_async( func=None, thread_sensitive=True, executor=None, ): if func
is None: return lambda f: SyncToAsync( f, thread_sensitive=thread_sensitive, executor=executor, ) return SyncToAsync( func, thread_sensitive=thread_sensitive, executor=executor, ) BTHJSFGTZODQZ -
SyncToAsync •ThreadPoolExecutorΛར༻͠ɺಉظؔΛΠϕϯτϧʔϓͰ࣮ߦ͢Δ class SyncToAsync: ... async def __call__(self, *args, **kwargs):
loop = asyncio.get_running_loop() ... executor = AsyncToSync.loop_thread_executors[loop] -
SyncToAsync •ThreadPoolExecutorΛར༻͠ɺಉظؔΛΠϕϯτϧʔϓͰ࣮ߦ͢Δ class SyncToAsync: ... async def __call__(self, *args, **kwargs):
loop = asyncio.get_running_loop() ... executor = AsyncToSync.loop_thread_executors[loop] future = loop.run_in_executor( executor, functools.partial( self.thread_handler, loop, self.get_current_task(), sys.exc_info(), func, *args, **kwargs, ), -
SyncToAsync •ThreadPoolExecutorΛར༻͠ɺಉظؔΛΠϕϯτϧʔϓͰ࣮ߦ͢Δ class SyncToAsync: ... async def __call__(self, *args, **kwargs):
loop = asyncio.get_running_loop() ... executor = AsyncToSync.loop_thread_executors[loop] future = loop.run_in_executor( executor, functools.partial( self.thread_handler, loop, self.get_current_task(), sys.exc_info(), func, *args, **kwargs, ), ΠϕϯτϧʔϓͰ&YFDVUPSΛ͏ -
DEP 0009: Async-capable Django •ʢDEPDjangoͷPEPʣ •Sequencing ͱͯ͠3ஈ֊ •First round (hopefully
in 3.0) •Further individual projects •Second round (hopefully in 3.1) IUUQTHJUIVCDPNEKBOHPEFQTCMPCNBJOBDDFQUFEBTZODSTU -
DEP 0009: Async-capable Django •ʢDEPDjangoͷPEPʣ •Sequencing ͱͯ͠3ஈ֊ •First round (hopefully
in 3.0) •Further individual projects •Second round (hopefully in 3.1) •ORM (async-wrapper interface around existing sync core) •طଘͷಉظίΞʹରԠͨ͠ඇಉظϥούʔΠϯλϑΣʔε •ORM (native async with sync wrappers for backwards compatibility) ޙํޓੑͷͨΊͷಉظϥούʔΛඋ͑ͨωΠςΟϒඇಉظ IUUQTHJUIVCDPNEKBOHPEFQTCMPCNBJOBDDFQUFEBTZODSTU -
DEP 0009: Async-capable Django •ʢDEPDjangoͷPEPʣ •Sequencing ͱͯ͠3ஈ֊ •First round (hopefully
in 3.0) •Further individual projects •Second round (hopefully in 3.1) •ORM (async-wrapper interface around existing sync core) •طଘͷಉظίΞʹରԠͨ͠ඇಉظϥούʔΠϯλϑΣʔε •ORM (native async with sync wrappers for backwards compatibility) ޙํޓੑͷͨΊͷಉظϥούʔΛඋ͑ͨωΠςΟϒඇಉظ IUUQTHJUIVCDPNEKBOHPEFQTCMPCNBJOBDDFQUFEBTZODSTU ࣮ॱং༧ఆͲ͓Γʂ -
4.1 ORM 🤔 •Θͨ͠ͷߟ͑Δཧ༝3ͭ •ޓੑ •ޓੑΛҡ࣋͢Δํ๏Ͱ࣮ΛਐΊ͍ͯΔ •ʢ૾ʣࢿۚӨڹ͕͋Δͷ͔ʁ •IF͔ΒରԠ͢Δͷॳͷ༧ఆͲ͓Γ •ඇಉظରԠͷϝΠϯ։ൃऀͷϦιʔεෆ •DEP
9 / asgiref ͷ Andrew Godwin ࢯ •DEPͦΕ΄Ͳ֬ݻͨΔ༷Ͱͳͦ͞͏ʢ໋໊نଇมߋʹͳͬͯΔ -
4.1 ORM 🤔 •Θͨ͠ͷߟ͑Δཧ༝3ͭ •ޓੑ •ޓੑΛҡ࣋͢Δํ๏Ͱ࣮ΛਐΊ͍ͯΔ •ʢ૾ʣࢿۚӨڹ͕͋Δͷ͔ʁ •IF͔ΒରԠ͢Δͷॳͷ༧ఆͲ͓Γ •ඇಉظରԠͷϝΠϯ։ൃऀͷϦιʔεෆ 🤔
•DEP 9 / asgiref ͷ Andrew Godwin ࢯ •PyConJP 2022ͷΩʔϊʔτͷʢFaster C Pythonʣ💰 •DEPͦΕ΄Ͳ֬ݻͨΔ༷Ͱͳͦ͞͏ʢ໋໊نଇมߋʹͳͬͯΔ •DEP 9 Ͱࢿۚௐୡͷग़͍ͯΔ Donationͷϖʔδ -
謝辞 •Asynchronous ORM •https://forum.djangoproject.com/t/asynchronous-orm/5925/21 •ݱͰ͑Δ Django ͷڭՊॻ - ԣ ໌ਔ(ஶ)
•https://akiyoko.booth.pm/items/1059917 •࣮ફDjango PythonʹΑΔຊ֨WebΞϓϦέʔγϣϯ։ൃ - ࣳా ক (ஶ) •https://www.shoeisha.co.jp/book/detail/9784798153964 • Async Django: The practical guide you've been *awaiting* for by Carlton Gibson - DjangoCon Europe 2022 •https://www.youtube.com/watch?v=Lfe2zsGS0Js • Keynote: State of the Object-Relational Mapping (ORM) - Simon Charette •https://2022.djangocon.us/talks/keynote-state-of-orm/