Cannot get Background Task to Start

Hello:
[note this App is connected via uplink code - to the local server side also]

I am trying to download and then transcribe an audio (or video) file. I have the code working to handle the transcription and it works from the Anvil App as long as the transcribing does not time out –

So using the background task tutorial I created the functionality to do it in a background task.

However I am not able to get the background task to do anything. It does not create a background task in the logging section or show any active background task running.

I tried to start the task from the anvil.server level (server module/web-side) and also tried to do it from callable functions at the server-side (local uplink connected callable code).

In the code below you will see a function called (as the background_task)
anvil.server.launch_background_task(‘downloadMediaFile’,file)

Please note that when I remove the background_task decorator and replace it with ‘callable’ decorator and run it as shown below - the function works and if the audio file is short enough, returns with the proper results.
anvil.server.call(‘downloadMediaFile’,file)

But when I try to run it as a background task – nothing at all happens at all - the form function times out and presents the message : “anvil.server.TimeoutError: Server code took too long”

I cloned the background_task example and it works but it is not involving the uplink side of this operation. I would expect to be able to print a message when the start_background method is called - so there is no way to prove that it even calls that function.

A frustrating hour trying to figure this out – likely it is right in front of me – thank you in advance for any help.

Code Sample:


## the background task method
@anvil.server.background_task
def downloadMediaFile(file):
    paths = db.get_site_paths('singletranscripts')
    code_path = paths['code_path']
    file_path = paths['file_path']
    print (f"doing the download ")
    maketranscript = single_transcript.CreateTranscriptFromFile()
    manager = maketranscript.manage_transcript(file,code_path,file_path)

## the calling method 
@anvil.server.callable
def start_file_transcribe_background_task(file):
print('in the starter')
  #results = anvil.server.call('downloadMediaFile',file)
  task = anvil.server.launch_background_task('downloadMediaFile',file)
  return task

## code in the form
print(f"The file's name is: {file.name}")
    print(f"The number of bytes in the file is: {file.length}")
    print(f"The file's content type is: {file.content_type}")
    self.title_of_file.text = f"Transcribing {file.name}"
    #results = anvil.server.call('downloadMediaFile',file)
    self.wait_message.text = 'Get Some Coffee This Will Take a Few Minutes ...'
    results = anvil.server.call('start_file_transcribe_background_task',file)
    
# paste your code between ``` 

Clone link:
share a copy of your app

I can’t tell you what’s wrong without clone link.

But I want to make it clear that results = anvil.server.call('start_file_transcribe_background_task',file) will store a background task object in results, not the values returned by the background task.

It would be clearer if it was task = anvil.server.call('start_file_transcribe_background_task',file)

The problem is the middleman: your call to the server function. Instead, call your uplink function directly from the client code. There’s no time limit on that, and that uplink callable function can call the background task itself when it’s done.

Thank you for your response. I have called the uplink function directly and it works – but I thought it would time out when having to wait for 5+ minutes. But I do not understand your instruction to call the background task when the long uplink function is finished. I may be misunderstanding but if call the function downloadMediaFile(file) – on the remote uplink server – and the web side will wait and spin for 5+ minutes then why do I need the background call – I think I just do not follow your instructions.

Thank you,

Jack

Sorry, nevermind then about my previous response. I guess I misunderstood. What did you mean when you said about the background_task tutorial: “but it is not involving the uplink side of this operation.” That’s the part that I guess I misinterpreted.

A call to uplink won’t timeout, as far as I know.

Thank you HT: I was able to get this to work without using background task at all - -but I am not clear why. My understanding of why Background Tasks are needed is that a long running process is started and if the primary web client waits it will time out. So expected to be able to start the long background task as a background_task and then using the returned task-object I could monitor it – perhaps build some sort of reminder to the user that the process was still running and then when it completed get access to the results.

As it is I can start the task and the web client just spins until it completes. It does not allow the user to do anything else - but in this case that is fine – But my understanding of the background task functionality must be flawed.

I set up the task as shown in the docs and tutorial and called the remote uplink function from a method that starts the background task (see code above) and nothing happened at all – no messages, not activity nothing. As though it just returned as complete and that is that.

I am continuing to learn to use this amazing product (anvil) and I want to use the background task for tasks that can notify me later while the user can access the application for other tasks – So that is why I am trying to understand how it works and why this does not and why I can call the long running task from the web-client directly on the uplink and there is no timeout?

Thank you,

Jack

Yes, that is how background tasks work.

Can you share a clone link to an app demonstrating this behavior? I have in mind just a simple app, maybe even the background task tutorial app, if it is behaving like that for you.

The code shared in your OP has some strange indentation in places and also some puzzling commented-out lines. Also, it’s not clear which part of it is uplink code or is calling an uplink function. So cleaning up that code might help us understand. But a clone link to an actual app where we can see the code in context is probably better.

Sure on all counts. :slight_smile: I will do that later today – Thanks for helping me out.

1 Like