The goal is to define and initialize a python class that acts as an interface to resources that involve blocking IO Operations during initialization, and normal program operation.
Based on a few other posts here, The best that I could come up with is as below, Is there a better way, and if not, why not?
class IOResourceInterface:
def __init__(self, url, config={}):
# Normal class initialization
# store URL(s) to resources (such as files, or Network bound resources)
# Store Configurations (like database creds, etc.)
pass
async def init_coro(self):
# Eagerly Initializing Connection to External Resources
await asyncio.sleep(1) # simulates the initialization of IO Resources
def __await__(self) -> "IOResourceInterface":
yield from self.init_coro().__await__()
return self
async def do_stuff(self):
pass
# And then within the event loop
resource = await IOResourceInterface("protocol://resource",config={"user":"", "pass":""})
# Here resource is fully initialized and ready to go
await resource.do_stuff()