I'm currently trying to do something like this:
import asyncio
class Dummy:
def method(self):
return 1
def __str__(self):
return "THIS IS A DUMMY CLASS"
async def start_doing():
asyncio.sleep(1)
return Dummy
async def do_something():
return start_doing().method()
async def main():
a = asyncio.create_task(do_something())
b = asyncio.create_task(do_something())
results = await asyncio.gather(a, b)
print(results)
asyncio.run(main())
But I get this error:
AttributeError: 'coroutine' object has no attribute 'method'
Which indicates that I cannot call my method on a coroutine object. One way to fix this is by doing these:
async def do_something():
return (await start_doing()).method()
But I think that by doing this you are inherently making your code synchronous. You are not generating a future, rather waiting for your work to be finished in do_something
and then proceed to the next item in the queue.
How should I call an object method in the future when the awaitable
has been awaited and my object is ready? how to schedule it to be called in the future?