You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: sources/platform/actors/development/programming_interface/basic_commands.md
+48-23Lines changed: 48 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,17 +12,16 @@ import TabItem from '@theme/TabItem';
12
12
13
13
---
14
14
15
-
The commands described in this section are expected to be called from within a context of a running Actor, both in a local environment or on the Apify platform.
15
+
This page covers essential commands for the Apify SDK in JavaScript & Python. These commands are designed to be used within a running Actor, either in a local environment or on the Apify platform.
16
16
17
-
## Actor initialization
17
+
## Initialize your Actor
18
18
19
-
First, the Actor should be initialized. During initialization, it prepares to receive events from the Apify platform. The Actor determines the machine and storage configuration and optionally purges the previous state from local storage. It will also create a default instance of the Actor class.
19
+
Before using any Apify SDK methods, initialize your Actor. This step prepares the Actor to receive events from the Apify platform, sets up machine and storage configurations, and clears previous local storage states.
20
20
21
-
It is not required to perform the initialization explicitly because the Actor will initialize on the execution of any Actor method, but we strongly recommend it to prevent race conditions.
22
-
23
-
### Using the JavaScript SDK
21
+
<TabsgroupId="main">
22
+
<TabItemvalue="JavaScript"label="JavaScript">
24
23
25
-
The Actor is initialized by calling the `init()` method. It should be paired with an `exit()`method, which terminates the Actor. The use of `exit()` is not required but recommended. For more information, go to [Exit Actor](#exit-actor).
24
+
Use the `init()` method to initialize your Actor. Pair it with `exit()`to properly terminate the Actor. For more information on `exit()`, go to [Exit Actor](#exit-actor).
An alternative way of initializing the Actor is with a `main()` function. This is useful in environments where the latest JavaScript syntax and top-level awaits are not supported. The main function is only syntax-sugar for `init()` and `exit()`. It will call `init()` before it executes its callback and `exit()` after the callback resolves.
35
+
Alternatively, use the `main()` function for environments that don't support top-level awaits. The `main()` function is syntax-sugar for `init()` and `exit()`. It will call `init()` before it executes its callback and `exit()` after the callback resolves.
36
+
37
37
38
38
```js
39
39
import { Actor } from'apify';
@@ -44,9 +44,10 @@ Actor.main(async () => {
44
44
});
45
45
```
46
46
47
-
### Using the Python SDK
47
+
</TabItem>
48
+
<TabItemvalue="Python"label="Python">
48
49
49
-
In the Python SDK, an Actor is written as an asynchronous context manager, which means that we can use the `with` keyword to write our Actor code into a block. The `init()` method will be called before the code block is executed, and the `exit()` method will be called after the code block is finished.
50
+
In Python, use an asynchronous context manager with the `with` keyword. The `init()` method will be called before the code block is executed, and the `exit()` method will be called after the code block is finished.
50
51
51
52
```python
52
53
from apify import Actor
@@ -57,11 +58,12 @@ async def main():
57
58
# ...
58
59
```
59
60
60
-
## Get input
61
+
</TabItem>
62
+
</Tabs>
61
63
62
-
Get access to the Actor input object passed by the user. It is parsed from a JSON file, which is stored by the system in the Actor's default key-value store. Usually, the file is called `INPUT`, but the exact key is defined in the `ACTOR_INPUT_KEY` environment variable.
64
+
## Get input
63
65
64
-
The input is an object with properties. If the Actor defines the input schema, the input object is guaranteed to conform to it. For details, see[Input and output](#input-and-output).
66
+
Access the Actor's input object, which is stored as a JSON file in the Actor's default key-value store. The input is an object with properties. If the Actor defines the input schema, the input object is guaranteed to conform to it. For details, check out[Input and output](#input-and-output).
65
67
66
68
<TabsgroupId="main">
67
69
<TabItemvalue="JavaScript"label="JavaScript">
@@ -94,11 +96,11 @@ async def main():
94
96
</TabItem>
95
97
</Tabs>
96
98
97
-
## Key-value store access
99
+
Usually, the file is called `INPUT`, but the exact key is defined in the `ACTOR_INPUT_KEY` environment variable.
98
100
99
-
Write and read arbitrary files using a storage called [Key-value store](../../../storage/key_value_store.md). When an Actor starts, by default, it is associated with a newly-created key-value store, which only contains one file with the input of the Actor (see [Get input](#get-input)).
101
+
## Key-value store access
100
102
101
-
The user can override this behavior and specify another key-value store or input key when running the Actor.
103
+
Use the [Key-value store](../../../storage/key_value_store.md) to read and write arbitrary files
102
104
103
105
<TabsgroupId="main">
104
106
<TabItemvalue="JavaScript"label="JavaScript">
@@ -148,9 +150,9 @@ async def main():
148
150
149
151
## Push results to the dataset
150
152
151
-
Larger results can be saved to append-only object storage called [Dataset](../../../storage/dataset.md). When an Actor starts, by default, it is associated with a newly-created empty default dataset. The Actor can create additional datasets or access existing datasets created by other Actors and use those as needed.
153
+
Store larger results in a [Dataset](../../../storage/dataset.md), an append-only object storage
152
154
153
-
Note that Datasets can optionally be equipped with the schema that ensures only certain kinds of objects are stored in them. See [Dataset schema file](../../../storage/dataset.md) for more details.
155
+
Note that Datasets can optionally be equipped with the schema that ensures only certain kinds of objects are stored in them.
154
156
155
157
<TabsgroupId="main">
156
158
<TabItemvalue="JavaScript"label="JavaScript">
@@ -183,7 +185,16 @@ async def main():
183
185
184
186
## Exit Actor
185
187
186
-
When the main Actor process exits (i.e. the Docker container stops running), the Actor run is considered finished, and the process exit code is used to determine whether the Actor has succeeded (exit code `0` leads to status `SUCCEEDED`) or failed (exit code not equal to `0` leads to status `SUCCEEDED`). In this case, the platforms set a status message to a default value like `Actor exit with exit code 0`, which is not very descriptive for the users.
188
+
When an Actor's main process terminates, the Actor run is considered finished. The process exit code determines Actor's final status:
189
+
190
+
- Exit code `0`: Status `SUCCEEDED`
191
+
- Exit code not equal to `0`: Status `FAILED`
192
+
193
+
By default, the platform sets a generic status message like _Actor exit with exit code 0_. However, you can provide more informative message using the SDK's exit methods.
194
+
195
+
### Basic exit
196
+
197
+
Use the `exit()` method to terminate the Actor with a custom status message:
187
198
188
199
<TabsgroupId="main">
189
200
<TabItemvalue="JavaScript"label="JavaScript">
@@ -215,6 +226,10 @@ async def main():
215
226
</TabItem>
216
227
</Tabs>
217
228
229
+
### Immediate exit
230
+
231
+
To exit immediately without calling exit handlers:
232
+
218
233
219
234
<TabsgroupId="main">
220
235
<TabItemvalue="JavaScript"label="JavaScript">
@@ -245,6 +260,9 @@ async def main():
245
260
</TabItem>
246
261
</Tabs>
247
262
263
+
### Failed exit
264
+
265
+
To indicate a failed run:
248
266
249
267
<TabsgroupId="main">
250
268
<TabItemvalue="JavaScript"label="JavaScript">
@@ -275,10 +293,15 @@ async def main():
275
293
</TabItem>
276
294
</Tabs>
277
295
278
-
An alternative and preferred way to exit an Actor is using the `exit` function in the SDK, as shown below. This has two advantages:
296
+
### Preferred exit methods
297
+
298
+
The SDK provides convenient methods for exiting Actors:
299
+
300
+
1. Use `exit()` with custom messages to inform users about the Actor's achievements or issues.
279
301
280
-
- You can provide a custom status message for users to tell them what the Actor achieved. On error, try to explain to users what happened, and most importantly, how they can fix the error. This greatly improves user experience.
281
-
- The system emits the `exit` event, which can be listened to and used by various components of the Actor to perform a cleanup, persist state, etc. Note that the caller of exit can specify how long the system should wait for all `exit` event handlers to complete before closing the process, using the `timeoutSecs` option. For details, see [System Events](#system-events).
302
+
2. The `exit()` method emits `exit` event allowing components to perform cleanup or state persistence.
303
+
304
+
Example of a failed exit using a shorthand method:
282
305
283
306
<TabsgroupId="main">
284
307
<TabItemvalue="JavaScript"label="JavaScript">
@@ -309,6 +332,9 @@ async def main():
309
332
</TabItem>
310
333
</Tabs>
311
334
335
+
### Exit event handlers (JavaScript only)
336
+
337
+
In JavaScript, you can register handlers for the `exit` event:
312
338
313
339
<TabsgroupId="main">
314
340
<TabItemvalue="JavaScript"label="JavaScript">
@@ -332,7 +358,6 @@ await Actor.exit();
332
358
333
359
```python
334
360
# 😔 Custom handlers are not supported in the Python SDK yet.
335
-
# 👉 Stay tuned and follow the news at https://apify.com/change-log
0 commit comments