Skip to main content

Actor lifecycle

Lifecycle methods

Initialization and cleanup

At the start of its runtime, the Actor needs to initialize itself, its event manager and its storages, and at the end of the runtime it needs to close these cleanly. The Apify SDK provides several options on how to manage this.

Actor.init() and Actor.exit()

The Actor.init() method initializes the Actor, the event manager which processes the Actor events from the platform event websocket, and the storage client used in the execution environment. It should be called before performing any other Actor operations.

The Actor.exit() method then exits the Actor cleanly, tearing down the event manager and the storage client. There is also the Actor.fail() method, which exits the Actor while marking it as failed.

src/main.py
from apify import Actor
from apify_shared.consts import ActorExitCodes

async def main():
await Actor.init()
try:
Actor.log.info('Actor input:', await Actor.get_input())
await Actor.set_value('OUTPUT', 'Hello, world!')
raise RuntimeError('Ouch!')
await Actor.exit()
except Exception as e:
Actor.log.exception('Error while running Actor')
await Actor.fail(exit_code=ActorExitCodes.ERROR_USER_FUNCTION_THREW, exception=e)

Context manager

So that you don't have to call the lifecycle methods manually, the Actor class provides a context manager, which calls the Actor.init() method on enter, the Actor.exit() method on a clean exit, and the Actor.fail() method when there is an exception during the run of the Actor.

This is the recommended way to work with the Actor class.

src/main.py
from apify import Actor

async def main():
async with Actor:
Actor.log.info('Actor input:', await Actor.get_input())
await Actor.set_value('OUTPUT', 'Hello, world!')
raise RuntimeError('Ouch!')

Rebooting an Actor

Sometimes, you want to restart your Actor to make it run from the beginning again. To do that, you can use the Actor.reboot() method. When you call it, the Apify platform stops the container of the run, and starts a new container of the same Actor with the same run ID and storages.

Don't do it unconditionally, or you might get the Actor in a reboot loop.

src/main.py
from apify import Actor

async def main():
async with Actor:
# TODO: figure out a good reason why to reboot
await Actor.reboot()

Actor status message

To inform you or the users running your Actors about the progress of their runs, you can set the status message for the run, which will then be visible in the run detail in Apify Console, or accessible through the Apify API.

To set the status message for the Actor run, you can use the Actor.set_status_message() method.

src/main.py
from apify import Actor

async def main():
async with Actor:
await Actor.set_status_message('Here we go!')
...
await Actor.set_status_message('So far so good...')
...
await Actor.set_status_message('Steady as she goes...')
...
await Actor.set_status_message('Almost there...')
...
await Actor.set_status_message('Phew! That was not that hard!')