Django

官网有形容

Django异步的变动

  1. 4.2相比于4.0的变动就是应用sync_to_async
  2. 最大的变动就是能够应用同步和异步互不影响

异步实现

models文件夹下的query文件

class BaseIterable:    def __init__(        self, queryset, chunked_fetch=False, chunk_size=GET_ITERATOR_CHUNK_SIZE    ):        self.queryset = queryset        self.chunked_fetch = chunked_fetch        self.chunk_size = chunk_size    async def _async_generator(self):        # Generators don't actually start running until the first time you call        # next() on them, so make the generator object in the async thread and        # then repeatedly dispatch to it in a sync thread.        sync_generator = self.__iter__()        def next_slice(gen):            return list(islice(gen, self.chunk_size))        while True:            chunk = await sync_to_async(next_slice)(sync_generator)            for item in chunk:                yield item            if len(chunk) < self.chunk_size:                break    # __aiter__() is a *synchronous* method that has to then return an    # *asynchronous* iterator/generator. Thus, nest an async generator inside    # it.    # This is a generic iterable converter for now, and is going to suffer a    # performance penalty on large sets of items due to the cost of crossing    # over the sync barrier for each chunk. Custom __aiter__() methods should    # be added to each Iterable subclass, but that needs some work in the    # Compiler first.    def __aiter__(self):        return self._async_generator()class ModelIterable(BaseIterable):    """Iterable that yields a model instance for each row."""    def __iter__(self):        代码省略class QuerySet(AltersData):    """Represent a lazy database lookup for a set of objects."""    def __init__(self, model=None, query=None, using=None, hints=None):        self._iterable_class = ModelIterable   def __aiter__(self):      # Remember, __aiter__ itself is synchronous, it's the thing it returns      # that is async!      async def generator():          await sync_to_async(self._fetch_all)()          for item in self._result_cache:              yield item      return generator()

论断

  • 就是__aiter__ 实现了async for 办法,具体的await obj是通过sync_to_async实现
  • 感觉ORM层面的异步和同步区别不大,毕竟在数据连贯层面没有变动
  • 临时看进去的就这么多,技术无限,后续持续跟进