Actor lifecycle
This guide explains how an Apify Actor starts, runs, and shuts down, describing the complete Actor lifecycle. For information about the core concepts such as Actors, the Apify Console, storages, and events, check out the Apify platform documentation.
Actor initialization
During initialization, the SDK prepares all the components required to integrate with the Apify platform. It loads configuration from environment variables, initializes access to platform storages such as the key-value store, dataset, and request queue, sets up event handling for platform events, and configures logging.
The recommended approach in Python is to use the global Actor
class as an asynchronous context manager. This approach automatically manages setup and teardown and keeps your code concise. When entering the context, the SDK loads configuration and initializes clients lazily—for example, a dataset is opened only when it is first accessed. If the Actor runs on the Apify platform, it also begins listening for platform events.
When the Actor exits, either normally or due to an exception, the SDK performs a graceful shutdown. It persists the final Actor state, stops event handling, and sets the terminal exit code together with the status message.
- Actor class with context manager
- Actor class with manual init/exit
import asyncio
from apify import Actor
async def main() -> None:
async with Actor:
# Get input
actor_input = await Actor.get_input()
Actor.log.info('Actor input: %s', actor_input)
# Your Actor logic here
data = {'message': 'Hello from Actor!', 'input': actor_input}
await Actor.push_data(data)
# Set status message
await Actor.set_status_message('Actor completed successfully')
if __name__ == '__main__':
asyncio.run(main())
import asyncio
from apify import Actor
async def main() -> None:
await Actor.init()
try:
# Get input
actor_input = await Actor.get_input()
Actor.log.info('Actor input: %s', actor_input)
# Your Actor logic here
data = {'message': 'Hello from Actor!', 'input': actor_input}
await Actor.push_data(data)
# Set status message
await Actor.set_status_message('Actor completed successfully')
finally:
await Actor.exit()
if __name__ == '__main__':
asyncio.run(main())
You can also create an Actor
instance directly. This does not change its capabilities but allows you to specify optional parameters during initialization, such as disabling automatic sys.exit()
calls or customizing timeouts. The choice between using a context manager or manual initialization depends on how much control you require over the Actor's startup and shutdown sequence.
- Actor instance with context manager
- Actor instance with manual init/exit
import asyncio
from datetime import timedelta
from apify import Actor
async def main() -> None:
actor = Actor(
event_listeners_timeout=timedelta(seconds=30),
cleanup_timeout=timedelta(seconds=30),
)
async with actor:
# Get input
actor_input = await actor.get_input()
actor.log.info('Actor input: %s', actor_input)
# Your Actor logic here
data = {'message': 'Hello from Actor instance!', 'input': actor_input}
await actor.push_data(data)
# Set status message
await actor.set_status_message('Actor completed successfully')
if __name__ == '__main__':
asyncio.run(main())
import asyncio
from datetime import timedelta
from apify import Actor
async def main() -> None:
actor = Actor(
event_listeners_timeout=timedelta(seconds=30),
cleanup_timeout=timedelta(seconds=30),
)
await actor.init()
try:
# Get input
actor_input = await actor.get_input()
actor.log.info('Actor input: %s', actor_input)
# Your Actor logic here
data = {'message': 'Hello from Actor!', 'input': actor_input}
await actor.push_data(data)
# Set status message
await actor.set_status_message('Actor completed successfully')
finally:
await actor.exit()
if __name__ == '__main__':
asyncio.run(main())
Error handling
Good error handling lets your Actor fail fast on critical errors, retry transient issues safely, and keep data consistent. Normally you rely on the async with Actor:
block—if it finishes, the run succeeds (exit code 0); if an unhandled exception occurs, the run fails (exit code 1).
The SDK provides helper methods for explicit control:
Actor.exit
- terminates the run successfully (default exit code 0).Actor.fail
- marks the run as failed (default exit code 1).
Any non-zero exit code is treated as a FAILED
run. You rarely need to call these methods directly unless you want to perform a controlled shutdown or customize the exit behavior.
Catch exceptions only when necessary - for example, to retry network timeouts or map specific errors to exit codes. Keep retry loops bounded with backoff and re-raise once exhausted. Make your processing idempotent so that restarts don't corrupt results. Both Actor.exit
and Actor.fail
perform the same cleanup, so complete any long-running persistence before calling them.
Below is a minimal context-manager example where an unhandled exception automatically fails the run, followed by a manual pattern giving you more control.
import asyncio
from apify import Actor
async def main() -> None:
async with Actor:
# Any unhandled exception triggers Actor.fail() automatically
raise RuntimeError('Boom')
if __name__ == '__main__':
asyncio.run(main())
If you need explicit control over exit codes or status messages, you can manage the Actor manually using Actor.init
, Actor.exit
, and Actor.fail
.
import asyncio
import random
from apify import Actor
async def do_work() -> None:
# Simulate random outcomes: success or one of two exception types.
outcome = random.random()
if outcome < 0.33:
raise ValueError('Invalid input data encountered')
if outcome < 0.66:
raise RuntimeError('Unexpected runtime failure')
# Simulate successful work
Actor.log.info('Work completed successfully')
async def main() -> None:
await Actor.init()
try:
await do_work()
except ValueError as exc: # Specific error mapping example
await Actor.fail(exit_code=10, exception=exc)
except Exception as exc: # Catch-all for unexpected errors
await Actor.fail(exit_code=91, exception=exc)
else:
await Actor.exit(status_message='Actor completed successfully')
if __name__ == '__main__':
asyncio.run(main())
Reboot
Rebooting (available on the Apify platform only) instructs the platform worker to restart your Actor from the beginning of its execution. Use this mechanism only for transient conditions that are likely to resolve after a fresh start — for example, rotating a blocked proxy pool or recovering from a stuck browser environment.
Before triggering a reboot, persist any essential state externally (e.g., to the key-value store or dataset), as all in-memory data is lost after reboot. The example below tracks a reboot counter in the default key-value store and allows at most three restarts before exiting normally.
import asyncio
from apify import Actor
async def main() -> None:
async with Actor:
# Use the KVS to persist a simple reboot counter across restarts.
kvs = await Actor.open_key_value_store()
reboot_counter = await kvs.get_value('reboot_counter', 0)
# Limit the number of reboots to avoid infinite loops.
if reboot_counter < 3:
await kvs.set_value('reboot_counter', reboot_counter + 1)
Actor.log.info(f'Reboot attempt {reboot_counter + 1}/3')
# Trigger a platform reboot; after restart the code runs from the beginning.
await Actor.reboot()
Actor.log.info('Reboot limit reached, finishing run')
if __name__ == '__main__':
asyncio.run(main())
Status message
Status messages are lightweight, human-readable progress indicators displayed with the Actor run on the Apify platform (separate from logs). Use them to communicate high-level phases or milestones, such as "Fetching list", "Processed 120/500 pages", or "Uploading results".
Update the status only when the user's understanding of progress changes - avoid frequent updates for every processed item. Detailed information should go to logs or storages (dataset, key-value store) instead.
The SDK optimizes updates by sending an API request only when the message text changes, so repeating the same message incurs no additional cost.
import asyncio
from apify import Actor
async def main() -> None:
async with Actor:
await Actor.set_status_message('Here we go!')
# Do some work...
await asyncio.sleep(3)
await Actor.set_status_message('So far so good...')
await asyncio.sleep(3)
# Do some more work...
await Actor.set_status_message('Steady as she goes...')
await asyncio.sleep(3)
# Do even more work...
await Actor.set_status_message('Almost there...')
await asyncio.sleep(3)
# Finish the job
await Actor.set_status_message('Phew! That was not that hard!')
if __name__ == '__main__':
asyncio.run(main())
Conclusion
This page has presented the full Actor lifecycle: initialization, execution, error handling, rebooting, shutdown and status messages. You've seen how the SDK supports both context-based and manual control patterns. For deeper dives, explore the reference docs, guides, and platform documentation.