Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 0 additions & 17 deletions docs/03-concepts/01-actor-lifecycle.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,6 @@ async def main():
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.
Expand Down
7 changes: 3 additions & 4 deletions docs/03-concepts/04-actor-events.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ and to remove them, you use the [`Actor.off()`](../../reference/class/Actor#off)

```python title="src/main.py"
import asyncio
from apify import Actor
from apify_shared.consts import ActorEventTypes
from apify import Actor, Event

async def main():
async with Actor:
Expand All @@ -94,7 +93,7 @@ async def main():
Actor.log.info('Saving Actor state', extra=event_data)
await Actor.set_value('STATE', processed_items)

Actor.on(ActorEventTypes.PERSIST_STATE, save_state)
Actor.on(Event.PERSIST_STATE, save_state)

# Do some fake work
for i in range(processed_items, total_items):
Expand All @@ -103,7 +102,7 @@ async def main():
await asyncio.sleep(0.1)

# Suppose we can stop saving the state now
Actor.off(ActorEventTypes.PERSIST_STATE, save_state)
Actor.off(Event.PERSIST_STATE, save_state)

# Do some more fake work, this time something that can't be restarted,
# so no point persisting the state
Expand Down
8 changes: 4 additions & 4 deletions docs/03-concepts/11-configuration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ or the list of environment variables that the Actor understands.
This will cause the Actor to persist its state every 10 seconds:

```python title="src/main.py"
from apify import Actor
from apify_shared.consts import ActorEventTypes
from datetime import timedelta
from apify import Actor, Event, Configuration

async def main():
global_config = Configuration.get_global_configuration()
global_config.persist_state_interval_millis = 10000
global_config.persist_state_interval = timedelta(seconds=10)

async with Actor:
async def save_state():
await Actor.set_value('STATE', 'Hello, world!')

# The `save_state` handler will be called every 10 seconds now
Actor.on(ActorEventTypes.PERSIST_STATE, save_state)
Actor.on(Event.PERSIST_STATE, save_state)
```

### Configuring via environment variables
Expand Down