Exception handling during asyncio tasks

Hello.

In following code example objs variable will be empty (the results of all tasks will be lost), because one of the tasks raised exception. How can I come over that? For example, so that one of the tasks returns a normal answer, and the other returns just None.

Thank you.

tasks_gather = asyncio.gather(
historical_pricing.events.Definition(universe = 'RUBONOR=',start = '2021-12-20 00:00',end = '2021-12-20 01:00').get_data_async(),
historical_pricing.events.Definition(universe = 'RUBONOR=',start = '2021-12-20 01:00',end = '2021-12-20 02:00').get_data_async())

try:
asyncio.get_event_loop().run_until_complete(tasks_gather)
objs = tasks_gather.result()
except rd.errors.RDError as e:
print("RDError code :", e.code)
print("RDError message:", e.message)


Best Answer

  • umer.nalla
    umer.nalla LSEG
    Answer ✓

    Hi @aleksandr.chernov1

    The RD Python lib team have advised something like the following:

    tasks_gather = asyncio.gather(historical_pricing.events.Definition(universe = 'RUBONOR=',
        start = '2021-12-20 00:00',
        end = '2021-12-20 01:00').get_data_async(),
        historical_pricing.events.Definition(universe = 'RUBONOR=',
        start = '2021-12-20 01:00',
        end = '2021-12-20 02:00').get_data_async(),
        return_exceptions=True
    )
    asyncio.get_event_loop().run_until_complete(tasks_gather)
    good_response_1, error_2 = tasks_gather.result()
    display(good_response_1.data.df)
    display(error_2.message, error_2.code)



Answers