import asyncio
class A:
def __init__(self):
self.mylist = []
@property
def show_list(self):
if not self.mylist:
asyncio.run(self.load_list()) # neither works?
asyncio.create_task(self.load_list())
return self.mylist
async def load_list(self):
await asyncio.sleep(1)
self.mylist = [1]
async def test():
f = A()
print(f.show_list)
asyncio.run(test())
I am trying to run a code like this, the main feature being show_list is a property of A, and contains some data to be loaded only when required. However, when running the snippet above, I get these errors:
RuntimeError: asyncio.run() cannot be called from a running event loop
sys:1: RuntimeWarning: coroutine 'A.load_list' was never awaited
How can I keep the property while loading the data on demand?