i face problem when listen abort event [python sdk]
I followed the official documentation's template code to implement an Apify Actor, built and deployed it on the Apify platform.
When I run the Actor and click Abort (graceful abort), the ABORTING event handler handler_foo does not log anything — Actor.log.info(...) inside it never appears.
I have no way to confirm whether the handler is actually being triggered, and I need to perform custom actions when the user aborts the run.
Questions:
How can I guarantee that the ABORTING event handler is called?
How can I ensure that logs and operations (like Actor.set_value) inside it execute and appear in the logs?
--------------------------------------------------------------------------------------------
from apify import Actor
from apify_shared.consts import ActorEventTypes
import asyncio
def handler_foo(arg: dict):
Actor.log.info(f'handler_foo: arg = {arg}')
def handler_boo(arg: dict):
pass
async def main():
async with Actor:
# Add event handler
Actor.on(ActorEventTypes.ABORTING, handler_foo)
await asyncio.sleep(5*1000)
# Remove all handlers for a specific event
Actor.off('systemInfo')
# Remove a specific event handler
Actor.off('systemInfo', handler_boo)
10 Replies
Hi @flash mage
Event.ABORTING is only executed if abort gracefully is executed - https://docs.apify.com/sdk/python/docs/concepts/actor-events#: ~:text=can%20choose%20to%20abort%20gracefully%20to%20allow%20the%20Actor%20some%20time%20before%20getting%20killed

@Mantisus Is there any way to detect whether a user has successfully executed and obtained data, or if there was an error, aborted, or other failure – and perform some processing before a successful execution?
If
aborted was performed not gracefully. As far as I know, you cannot handle this.
You can catch errors using try.. except..Can I check if my user successfully ran the Actor and retrieved the results? and no abort happened
I think this is something you can also do with try.. except..
Or is there a specific case that cannot be handled this way?
Also note that you must handle the
MIGRATING event, which may be triggered for your actor and cause it to restart.For example, with on-demand jobs, users can abort the Actor even after the service has started — if no results are generated, no cost is charged.
if no results are generated, no cost is charged.It looks like a task for Pay-Per-Event. But I'm not deeply enough immersed in this topic.
I might need to research something like pay-per-event API calls. I haven't used pay-per-event before — do you know if a user aborts after calling the API, will they still be charged?
Hi @flash mage , you might be interested in the PPE (Pay Per Event) and PPR (Pay Per Result); PPR is a subset of PPE. You can check out the platform documentation here - https://docs.apify.com/platform/actors/publishing/monetize/pay-per-result and the PPE guide in the SDK docs - https://docs.apify.com/sdk/python/docs/concepts/pay-per-event.
ok, thanks for your help @Mantisus @Vlada Dusek