Interacting with other Actors
There are several methods that interact with other Actors and Actor tasks on the Apify platform.
Actor start
The Actor.start
method starts another Actor on the Apify platform, and immediately returns the details of the started Actor run.
from apify import Actor
async def main() -> None:
async with Actor:
# Start your own Actor named 'my-fancy-actor'.
actor_run = await Actor.start(
actor_id='~my-fancy-actor',
run_input={'foo': 'bar'},
)
# Log the Actor run ID.
Actor.log.info(f'Actor run ID: {actor_run.id}')
Actor call
The Actor.call
method starts another Actor on the Apify platform, and waits for the started Actor run to finish.
from apify import Actor
async def main() -> None:
async with Actor:
# Start the apify/screenshot-url Actor.
actor_run = await Actor.call(
actor_id='apify/screenshot-url',
run_input={'url': 'http://example.com', 'delay': 10000},
)
if actor_run is None:
raise RuntimeError('Actor task failed to start.')
# Wait for the Actor run to finish.
run_client = Actor.apify_client.run(actor_run.id)
await run_client.wait_for_finish()
# Get the Actor output from the key-value store.
kvs_client = run_client.key_value_store()
output = await kvs_client.get_record('OUTPUT')
Actor.log.info(f'Actor output: {output}')
Actor call task
The Actor.call_task
method starts an Actor task on the Apify platform, and waits for the started Actor run to finish.
from apify import Actor
async def main() -> None:
async with Actor:
# Start the Actor task by its ID.
actor_run = await Actor.call_task(task_id='Z3m6FPSj0GYZ25rQc')
if actor_run is None:
raise RuntimeError('Actor task failed to start.')
# Wait for the task run to finish.
run_client = Actor.apify_client.run(actor_run.id)
await run_client.wait_for_finish()
# Get the task run dataset items
dataset_client = run_client.dataset()
items = await dataset_client.list_items()
Actor.log.info(f'Task run dataset items: {items}')
Actor metamorph
The Actor.metamorph
operation transforms an Actor run into a run of another Actor with a new input. This feature is useful if you want to use another Actor to finish the work of your current Actor, instead of internally starting a new Actor run and waiting for its finish. With metamorph, you can easily create new Actors on top of existing ones, and give your users nicer input structure and user interface for the final Actor. For the users of your Actors, the metamorph operation is completely transparent; they will just see your Actor got the work done.
Internally, the system stops the container corresponding to the original Actor run and starts a new container using a different container image. All the default storages are preserved,and the new Actor input is stored under the INPUT-METAMORPH-1
key in the same default key-value store.
To make you Actor compatible with the metamorph operation, use Actor.get_input
instead of Actor.get_value('INPUT')
to read your Actor input. This method will fetch the input using the right key in a case of metamorphed run.
For example, imagine you have an Actor that accepts a hotel URL on input, and then internally uses the apify/web-scraper
public Actor to scrape all the hotel reviews. The metamorphing code would look as follows:
from apify import Actor
async def main() -> None:
async with Actor:
# Get the original Actor input.
actor_input = await Actor.get_input() or {}
hotel_url = actor_input.get('hotel_url')
# Create new input for apify/web-scraper Actor.
web_scraper_input = {
'startUrls': [{'url': hotel_url}],
'pageFunction': """async function pageFunction(context) {
// Here you pass the JavaScript page function
// that scrapes all the reviews from the hotel's URL
}""",
}
# Metamorph the Actor run to `apify/web-scraper` with the new input.
await Actor.metamorph('apify/web-scraper', web_scraper_input)
# This code will not be called, since the `metamorph` action terminates
# the current Actor run container.
Actor.log.info('You will not see this!')