---
title: Actor lifecycle
sidebar_label: 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()`](../../reference/class/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()`](../../reference/class/Actor#exit) method then exits the Actor cleanly,
tearing down the event manager and the storage client.
There is also the [`Actor.fail()`](../../reference/class/Actor#fail) method, which exits the Actor while marking it as failed.

```python title="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`](../../reference/class/Actor) class provides a context manager,
which calls the [`Actor.init()`](../../reference/class/Actor#init) method on enter,
the [`Actor.exit()`](../../reference/class/Actor#exit) method on a clean exit,
and the [`Actor.fail()`](../../reference/class/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.

```python title="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!')
```

#### Main function

Another option is to pass a function to the Actor via the [`Actor.main(main_func)`](../../reference/class/Actor#main) method,
which causes the Actor to initialize, run the main function, and exit, catching any runtime errors in the passed function.

```python title="src/main.py"
from apify import Actor

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

async def main():
    await Actor.main(actor_main_func)
```

### 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()`](../../reference/class/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.

```python title="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()`](../../reference/class/Actor#set_status_message) method.

```python title="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!')
```
