Apify Discord Mirror

Updated 8 months ago

Using the Actor instance across multiple files

At a glance

The community member is asking how to use the same Actor instance across multiple functions defined in separate files. They provide an example of defining an Actor in the main.py file and wanting to use Actor.log.info() in another function called from the main script.

In the comments, another community member suggests that the Actor instance can be used everywhere by importing it, as it behaves like a singleton. After calling async with Actor: (some code) in the main script, the same Actor instance can be used in all imports.

The original community member confirms that they have observed the same instance ID across different files, which aligns with the suggestion provided in the comments.

Hi, how can i use the same actor instance across multiple functions defined in seperate files?

for example, if I define an Actor in the main.py file as:

async with Actor:
(some code)


How do i use Actor.log.info() in another function that is called from the main script?

Would appreciate any help in this.
A
M
D
3 comments
just advanced to level 1! Thanks for your contributions! πŸŽ‰
Hey

just use it everywhere.
Plain Text
from apify import Actor

def my_function():
  Actor.log.info()

Think of it as a singleton
After calling
Plain Text
async with Actor:
     (some code)

In all imports you will refer to this particular Actor
This is what I was wondering. As I checked the instance id across different files, they were all the same.
Thank you for confirming
Add a reply
Sign up and join the conversation on Discord