I am debugging a background task by running it as a function from the server console. The function crashes when it tries to update its task status, so I have done this little change:
# old version
anvil.server.task_state['progress'] = 'Doing something...'
# new version
update_status('Doing something...')
def update_status(text):
if anvil.server.context.type == 'background_task':
anvil.server.task_state['progress'] = text
elif anvil.server.context.type == 'server_module':
print(text)
else:
raise Exception(f'Unexpected context type: {anvil.server.context.type}')
I thought this would print to console when running from console and would update the task status when running as background task, but it always prints, and I end up with task_state['progress']
never set and with all the info printed to the background task session log.
According to the documentation, when running in a background task it should be anvil.server.context.type == 'background_task'
, but it looks like it is always anvil.server.context.type == 'server_module'
.
What am I missing?