关于django:django42-异步ORM的变化持续观察

10次阅读

共计 1681 个字符,预计需要花费 5 分钟才能阅读完成。

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 层面的异步和同步区别不大,毕竟在数据连贯层面没有变动
  • 临时看进去的就这么多,技术无限,后续持续跟进
正文完
 0