Issue
I have a class User
which needs to be iterated at the asyncio script.
What is the correct way to convert User.objcets.iterator()
to async iterator with __aiter__
...?
Solution
You can create such iterator using async generator(it has aiter function):
async def sync_to_async(iterator):
for i in iterator:
yield i
Also output from console
>>> it = Chat.objects.iterator()
>>> ait = sync_to_async(it)
>>> dir(ait)
['__aiter__', '__anext__', '__class__', '__del__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__name__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'aclose', 'ag_await', 'ag_code', 'ag_frame', 'ag_running', 'asend', 'athrow']
Answered By - Anton Pomieshchenko
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.