Standby mode
Use Actors as an API server for fast response times.
Traditional Actors are designed to run a single task and then stop. They're mostly intended for batch jobs, such as when you need to perform a large scrape or data processing task. However, in some applications, waiting for an Actor to start is not an option. Actor Standby mode solves this problem by letting you have the Actor ready in the background, waiting for the incoming HTTP requests. In a sense, the Actor behaves like a real-time web server or standard API server.
Developing Actors using Standby mode
The best way to start developing Standby Actors is to use the predefined templates in the Console UI or in CLI via apify create
. The templates contain minimal code to get you up to speed for development in JavaScript, TypeScript or Python. Standby mode will automatically be enabled with default settings.
If you already have an existing Actor, or you just want to tweak the configuration of Standby mode, you can head to the Settings tab of your Actor, where the Actor Standby settings are located.
Actors using Standby mode must run a HTTP server listening on a specific port. The user requests will then be proxied to the HTTP server. You can use any of the existing HTTP request methods like GET, POST, PUT, DELETE, etc. You can pass the input via HTTP request query string or via HTTP request body.
Sometimes, you want the HTTP server to listen on a specific port and cannot change it yourself. You can use ACTOR_WEB_SERVER_PORT
environment variable to override the port so that Actor Standby will work with your code.
You can get the port using the Actor configuration available in Apify SDK. See example below with a simple Actor using Standby mode.
- JavaScript
- Python
import http from 'http';
import { Actor } from 'apify';
await Actor.init();
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello from Actor Standby!\n');
});
server.listen(Actor.config.get('containerPort'));
from http.server import HTTPServer, SimpleHTTPRequestHandler
from apify import Actor
class GetHandler(SimpleHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.end_headers()
self.wfile.write(b'Hello from Actor Standby!')
async def main() -> None:
async with Actor:
with HTTPServer(('', Actor.config.web_server_port), GetHandler) as http_server:
http_server.serve_forever()
Please make sure to describe your Actors, their endpoints, and the schema for their inputs and outputs in your README.
Determining an Actor is started in Standby
Actors that support Actor Standby can still be started in standard mode, for example from the Console or via the API.
To find out in which mode was the Actor started, you can read the metaOrigin
option in Actor.config
, or the APIFY_META_ORIGIN
environment variable in case you're not using the Apify SDK.
If it is equal to STANDBY
, the Actor was started in Standby mode, otherwise it was started in standard mode.
- JavaScript
- Python
import { Actor } from 'apify';
await Actor.init();
if (Actor.config.get('metaOrigin') === 'STANDBY') {
// Start your Standby server here
} else {
// Perform the standard Actor operations here
}
from apify import Actor
async def main() -> None:
async with Actor:
if Actor.config.meta_origin == 'STANDBY':
# Start your Standby server here
else:
# Perform the standard Actor operations here
Getting the URL of the Standby Actor
The URL is exposed as an environment variable ACTOR_STANDBY_URL
. You can also use Actor.config
, where the standbyUrl
option is available.
Monetization of Actors with the Standby mode?
Currently, the Standby mode is in beta, and monetization is not supported. But we plan to enable it soon.