fork download
  1. import asyncio
  2.  
  3. class AsyncDescriptor:
  4. @staticmethod
  5. async def fetch(url):
  6. print('fetching', url)
  7. # For demo just return:
  8. return 'contents of page at the specified URL'
  9.  
  10. def __init__(self, url):
  11. self._url = url
  12.  
  13. def __get__(self, obj, cls=None):
  14. # Generate some async future here
  15. return self.fetch(self._url)
  16.  
  17. class Test:
  18. value = AsyncDescriptor('https://e...content-available-to-author-only...e.com')
  19.  
  20.  
  21. async def main():
  22. test = Test()
  23. coroutine = test.value # a coroutine that can be schduled as a task
  24. # or simply awaited
  25. loop = asyncio.get_running_loop()
  26. task = loop.create_task(coroutine)
  27. ... # schedule other tasks perhaps?
  28. result = await task
  29. print(result)
  30.  
  31. if __name__ == '__main__':
  32. asyncio.run(main())
Success #stdin #stdout 0.08s 15764KB
stdin
Standard input is empty
stdout
fetching https://e...content-available-to-author-only...e.com
contents of page at the specified URL