Background tasks. My background task has a function that is outside the background function

I have created an instance of a class actually it’s the tweepy streaming class and modified the on_tweet function

Streaming_client = myStreamingClient(mytoken)
After authenticating I run my instance and call the filter function as follows
Streaming_client.filter()
The challenge is that the class myStreamingClient that has the on_tweet function is outside the background function. I only put the streaming.filter that will call the on_tweet when ever there is a tweet . This works perfectly outside anvil.
Now I am not sure what to do with the class and I am not sure if the on_tweet function will be called since it’s outside the background tasks. If I run I am getting connected to tweetter and verifying my steaming uses created by anvil even outside the anvil environment but cannot see my filtered tweets. New to anvil and I have a personal plan

I’m not sure what you mean by having a class defined

(emphasis mine). Could you give an example of “outside” and “inside”?

A background task should be able to call any server-side Python code in the app, whether it is written as a class or as a function.

Let me put the code here . The launch_back_task is called from client side
@anvil.server.callable
def launch_back_task():
client = getClient()
screen_name = ‘xxxxxxxx’
print(screen_name)
streaming_client = myStreamingClient(BREARER_TOKEN)
streaming_client.add_rules(tweepy.StreamRule(screen_name))
print(streaming_client.get_rules())
task = anvil.server.launch_background_task(‘load_module’)

return task

class myStreamingClient(tweepy.StreamingClient):
def on_tweet(self, tweet):
tweet_dict ={}
clean_tweet = tweetClean(tweet.text)
print(‘h’)
print(clean_tweet)
#print(tweet.id)
twt = getTweet(client, tweet.id)

@anvil.server.background_task
def load_module():
print(streaming_client.filter())

I see. This has nothing to do with the class definition being located outside of your functions. This has to do with some assumptions about how background tasks actually work. Here’s what’s going on:

  1. When the Client calls launch_back_task(), via anvil.server.call, the Anvil server starts up a new Python program, containing all your app’s server-side code, and runs it, providing a run-time environment where launch_back_task can run. This program instance exists until the function times out or returns.
  2. When launch_back_task calls load_module(), via anvil.server.launch_background_task, the same thing happens. The Anvil server starts up a new Python program, containing all your app’s server-side code, and runs it, providing a run-time environment where load_module can run. This program instance exists until the function times out or returns.

These are two separate programs, possibly running on two separate server PCs. They do not share memory, so they cannot share variables.

Even if they did, launch_back_task() creates streaming_client as a function-local variable. It is not visible to any other function, and exists only until this invocation of launch_back_task() ends. In particular, this variable is not visible to, and not in the memory of, the program where load_module will be running.

These are two completely separate memory spaces. They just happen to have (their own copies of) the same code. Not the same data.

In theory, you could pass streaming_client as a parameter to load_module. However, Anvil is somewhat limited in the kinds of objects that can be passed between different programs. (Yes, there are workarounds, for many cases.) And, since the sender and receiver don’t share memory, the only way to pass an object is by copying it.

So, in practice, it would probably be best for load_module to construct its own streaming_client, using BEARER_TOKEN, and whatever other information it needs, that can be passed from launch_back_task. This variable will exist inside the running load_module, will be visible there, and will live long enough to do what load_module asks of it.

Thank let me try that and see if it works