question

Upvotes
Accepted
7 2 1 5

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)


exceptionasynchronous
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

· Write an Answer
Upvote
Accepted
25.3k 87 12 25

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)



icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Thank you!

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.