Here’s the scenario - I have a server function that wants to start a background task. I don’t need to check its progress, but I do need to know if it actually started.
How do I do this reliably?
Testing shows that I need to put a delay between the call to start the task and checking the status properties and methods. This seems a little haphazard as the delay required might be dependent on other factors.
Client side is easy - you can start a timer to check progress (and in my testing the time it takes to return the task object to the client is sufficient delay for everything to work properly anyway).
If the delay is the only way, is there any way I can determine how long it should be?
I need to do some more testing on this - I’m beginning to wonder if I’ve just confused myself …
edit -
ok, further testing shows that get_termination_status() shows failed immediately after the background task function is launched, so I think I can use that. The others show incorrect values if checked immediately and throw exceptions if checked after a second or so delay.
edit 2 -
nope, it’s still a race. This shows None mostly but not always :
@anvil.server.background_task
def backtask():
print("In the backtask")
a = doesnotexist()
@anvil.server.callable
def test3():
xx = anvil.server.launch_background_task('backtask')
print("term_state",xx.get_termination_status())
and this shows failed pretty reliably :
@anvil.server.callable
def test3():
xx = anvil.server.launch_background_task('backtask')
time.sleep(0.5)
print("term_state",xx.get_termination_status())
So of course there are workarounds, therefore it’s not a problem per se, but I wonder what Anvil expects us to see after launching the task (ie is this intended behaviour or an idiosyncrasy)?