mirror of
https://github.com/panda3d/panda3d.git
synced 2025-09-30 00:32:57 -04:00
Loader: clean up callback awaiter code via Python 3.6+ syntax
This commit is contained in:
parent
a64dcd7c5d
commit
7b5280c0dd
@ -31,42 +31,6 @@ class Loader(DirectObject):
|
|||||||
# This indicates that this class behaves like a Future.
|
# This indicates that this class behaves like a Future.
|
||||||
_asyncio_future_blocking = False
|
_asyncio_future_blocking = False
|
||||||
|
|
||||||
class _ResultAwaiter(object):
|
|
||||||
"""Reinvents generators because of PEP 479, sigh. See #513."""
|
|
||||||
|
|
||||||
__slots__ = 'requestList', 'index'
|
|
||||||
|
|
||||||
def __init__(self, requestList):
|
|
||||||
self.requestList = requestList
|
|
||||||
self.index = 0
|
|
||||||
|
|
||||||
def __await__(self):
|
|
||||||
return self
|
|
||||||
|
|
||||||
def __anext__(self):
|
|
||||||
if self.index >= len(self.requestList):
|
|
||||||
raise StopAsyncIteration
|
|
||||||
return self
|
|
||||||
|
|
||||||
def __iter__(self):
|
|
||||||
return self
|
|
||||||
|
|
||||||
def __next__(self):
|
|
||||||
i = self.index
|
|
||||||
request = self.requestList[i]
|
|
||||||
if not request.done():
|
|
||||||
return request
|
|
||||||
|
|
||||||
self.index = i + 1
|
|
||||||
|
|
||||||
result = request.result()
|
|
||||||
if isinstance(result, PandaNode):
|
|
||||||
result = NodePath(result)
|
|
||||||
|
|
||||||
exc = StopIteration(result)
|
|
||||||
exc.value = result
|
|
||||||
raise exc
|
|
||||||
|
|
||||||
def __init__(self, loader, numObjects, gotList, callback, extraArgs):
|
def __init__(self, loader, numObjects, gotList, callback, extraArgs):
|
||||||
self._loader = loader
|
self._loader = loader
|
||||||
self.objects = [None] * numObjects
|
self.objects = [None] * numObjects
|
||||||
@ -124,13 +88,15 @@ class Loader(DirectObject):
|
|||||||
|
|
||||||
if self.requests:
|
if self.requests:
|
||||||
self._asyncio_future_blocking = True
|
self._asyncio_future_blocking = True
|
||||||
|
while self.requests:
|
||||||
|
yield self
|
||||||
|
|
||||||
if self.gotList:
|
if self.gotList:
|
||||||
return self._ResultAwaiter([self])
|
return self.objects
|
||||||
else:
|
else:
|
||||||
return self._ResultAwaiter(self.requestList)
|
return self.objects[0]
|
||||||
|
|
||||||
def __aiter__(self):
|
async def __aiter__(self):
|
||||||
""" This allows using `async for` to iterate asynchronously over
|
""" This allows using `async for` to iterate asynchronously over
|
||||||
the results of this class. It does guarantee to return the
|
the results of this class. It does guarantee to return the
|
||||||
results in order, though, even though they may not be loaded in
|
results in order, though, even though they may not be loaded in
|
||||||
@ -138,7 +104,8 @@ class Loader(DirectObject):
|
|||||||
requestList = self.requestList
|
requestList = self.requestList
|
||||||
assert requestList is not None, "Request was cancelled."
|
assert requestList is not None, "Request was cancelled."
|
||||||
|
|
||||||
return self._ResultAwaiter(requestList)
|
for req in requestList:
|
||||||
|
yield await req
|
||||||
|
|
||||||
# special methods
|
# special methods
|
||||||
def __init__(self, base):
|
def __init__(self, base):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user