Pyhon SDK on Aborting

I am trying to catch ABORTING event with the following code, without success. Could someone teach me how to do it properly ? Thanks 😆
import asyncio
from apify import Actor
from apify.consts import ActorEventTypes

async def on_abort(event_data):
print('### ABORTED', event_data)

async def main():
async with Actor:
print('INIT')

Actor.on(ActorEventTypes.ABORTING, on_abort)

print('sleeping ...')
await asyncio.sleep(10) ## PRESS ABORT

actor_input = await Actor.get_input()
print(actor_input)

print('DONE')

if __name__ == '__main__':
asyncio.run(main())
import asyncio
from apify import Actor
from apify.consts import ActorEventTypes

async def on_abort(event_data):
print('### ABORTED', event_data)

async def main():
async with Actor:
print('INIT')

Actor.on(ActorEventTypes.ABORTING, on_abort)

print('sleeping ...')
await asyncio.sleep(10) ## PRESS ABORT

actor_input = await Actor.get_input()
print(actor_input)

print('DONE')

if __name__ == '__main__':
asyncio.run(main())
the on_abort function never called, this is the output:
2023-04-05T19:03:20.022Z ACTOR: Pulling Docker image from repository.
2023-04-05T19:03:20.191Z ACTOR: Creating Docker container.
2023-04-05T19:03:20.324Z ACTOR: Starting Docker container.
2023-04-05T19:03:24.127Z INIT
2023-04-05T19:03:24.129Z sleeping ...
2023-04-05T19:03:28.185Z ACTOR: The actor run was aborted by the user.
2023-04-05T19:03:20.022Z ACTOR: Pulling Docker image from repository.
2023-04-05T19:03:20.191Z ACTOR: Creating Docker container.
2023-04-05T19:03:20.324Z ACTOR: Starting Docker container.
2023-04-05T19:03:24.127Z INIT
2023-04-05T19:03:24.129Z sleeping ...
2023-04-05T19:03:28.185Z ACTOR: The actor run was aborted by the user.
4 Replies
rival-black
rival-black•3y ago
Passed the questions to our Python devs, will get back once there's some response
!!!Joefree!!! 👑
thanks
rival-black
rival-black•3y ago
So - you have to abort gracefully for the event to be emitted:
No description
!!!Joefree!!! 👑
Ohh.. got it! now I understand. Thanks @Andrey Bykov

Did you find this page helpful?