Skip to main content

Actor lifecycle

In this guide, we will show you how to manage the lifecycle of an Apify Actor.

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.

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.

from apify import Actor


async def main() -> None:
await Actor.init()

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

except Exception as exc:
Actor.log.exception('Error while running Actor')
await Actor.fail(exit_code=91, exception=exc)

await Actor.exit()

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.

from apify import Actor


async def main() -> None:
async with Actor:
actor_input = await Actor.get_input()
Actor.log.info('Actor input: %s', actor_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.

from apify import Actor


async def main() -> None:
async with Actor:
# ... your code here ...
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.

from apify import Actor


async def main() -> None:
async with Actor:
await Actor.set_status_message('Here we go!')
# Do some work...
await Actor.set_status_message('So far so good...')
# Do some more work...
await Actor.set_status_message('Steady as she goes...')
# Do even more work...
await Actor.set_status_message('Almost there...')
# Finish the job
await Actor.set_status_message('Phew! That was not that hard!')