I’m having a problem to establish the communication with the backgroun task.
- start the task on client side
- return task to the client side and start timer
- let timer check the status and progress.
For my previous background tasks I was using database to store the data from the task. But now I would like to use it right away. For some reason I can’t get anything back from the task.
It’s always None
I’ve tried so far:
anvil.server.task_state['progress'] = 'yes'
server side
client side:state = self.task.get_state() test = state.get('progress')
just some pointless message to test it. returning only None or default value if setfound_rows = self.task.get_return_value()
If I get it correctly, it should return me the return from the background task? The result was too always onlyNone
. The task was completed so it shouldn’t be None.
get_return_value()
: returns the return value of the task, orNone
if the task has not yet returned.
client side:
class progressminature(progressminatureTemplate):
def __init__(self, **properties):
# Set Form properties and Data Bindings.
self.init_components(**properties)
list_barcodes = common.list_to_search_scan_in_tool
barcode_type = common.search_type_barcode
task = anvil.server.call('get_sample_database_scan_in_tool', list_barcodes, barcode_type)
self.task = task
def timer_1_tick(self, **event_args):
"""This method is called Every [interval] seconds. Does not trigger if [interval] is 0."""
with anvil.server.no_loading_indicator:
# Show progress
state = self.task.get_state()
test = state.get('progress')
print (test)
if self.task.is_running():
self.timer_1.interval = 0
#found_rows = state.get('date')
found_rows = self.task.get_return_value()
print (found_rows)
server side:
@anvil.server.callable
def get_sample_database_scan_in_tool(list_barcodes, barcode_type):
task = anvil.server.launch_background_task('background_get_sample_database_scan_in_tool',list_barcodes, barcode_type)
return task
@anvil.server.background_task
def background_get_sample_database_scan_in_tool(list_barcodes, barcode_type):
"""fetch the basic data of samples"""
anvil.server.task_state['progress'] = 'yes'
if barcode_type == 'internal':
database = app_tables.sample_database.search(q.fetch_only("barcode", "study_id","name","status","matrix","ext_barcode",
"volume","order_number","comment",updated_by=q.fetch_only("email")),barcode=q.any_of(*list_barcodes))
total_count = len(database)#total count found
elif barcode_type == 'external':
database = app_tables.sample_database.search(q.fetch_only("barcode", "study_id","name","status","matrix","ext_barcode",
"volume","order_number","comment",updated_by=q.fetch_only("email")),ext_barcode=q.any_of(*list_barcodes))
total_count = len(database)#total count found
return database