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.
# Start your own Actor named 'my-fancy-actor'
actor_run_details = await Actor.start('~my-fancy-actor', {'foo': 'bar'})
print(f'Started run ID: {actor_run_details["id"]}')
Actor.call()
The Actor.call()
method starts another Actor on the Apify platform,
and waits for the started Actor run to finish.
# Start the `apify/screenshot-url`, wait for it to finish, and get its output
actor_run_details = await Actor.call(
'apify/screenshot-url',
{'url': 'http://example.com', 'delay': 10000 },
)
run_client = Actor.apify_client.run(actor_run_details['id'])
screenshot = await run_client.key_value_store().get_value('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.
# Start the Actor task with ID `Z3m6FPSj0GYZ25rQc`,
# wait for it to finish, and get its dataset items
task_run_details = await Actor.call_task('Z3m6FPSj0GYZ25rQc')
run_client = Actor.apify_client.run(task_run_details['id'])
task_run_dataset_items = await run_client.dataset().list_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():
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`
web_scraper_input = {
'startUrls': [{ url: hotelUrl }],
'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
print('You will not see this!')