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层面的异步和同步区别不大,毕竟在数据连接层面没有变化
  • 暂时看出来的就这么多,技术有限,后续继续跟进

莫愁
95 声望2 粉丝