I am using python 3.8 and I want to write a function which gets executed every 5 seconds and sets a variable x from the response of an external API.
I have written a sample program which is giving me this error:
Exception in dummy API: '_asyncio.Task' object has no attribute 'context'
Code:
main.py
if __name__ == '__main__':
config = CONFIG.config
loop = asyncio.get_event_loop()
loop.run_until_complete(init_db(config))
# Using ensure_future, as I don't want to block here,
# want to execute the remaining piece of code as well.
asyncio.ensure_future(ABC.abc(), loop=loop)
# init cache
init_cache(config)
Host.host = config['HOST']
Host.port = get_port()
Host.name = CONFIG.static['service_name']
Host.attach_handlers(get_request_handlers())
Host.attach_web_middlewares([exception_handler])
Host._apm = config.get('APM', {}).get('ENABLED', False)
Host.run()
class ABC:
@classmethod
async def abc(cls):
x = await XYZClient.func()
await asyncio.sleep(5)
await cls.abc()
class XYZClient(APIClient):
@classmethod
async def func(cls):
path = "/v4/dummy_api"
result = dict()
try:
result = await cls.get(path)
except Exception as ex:
cls.logger.info("Exception in dummy API: {}".format(str(ex)))
return result
return result.get('data') or dict()
I am not getting where I am going wrong.
Any help would be highly appreciated.