Search code examples
pythonflaskpython-multithreadingfuture

flask_executor future.result() throws TypeError: 'str' object is not callable


I'm trying to get result from the process that I created to run in the background using flask_executor. This are methods in use:

    def scan_and_collect( self ):
    self.data_collector.collect()
    self.clearing_requester.request()
    return "Whatever!"

def scan_and_collect_async( self ):
    task_id = uuid.uuid4().hex
    self.executor.submit_stored(task_id, self.scan_and_collect())
    return jsonify({'task_id': task_id})

def task_status(self, task_id):
    if not self.executor.futures.done(task_id):
        future_status = self.executor.futures._state(task_id)
        return jsonify({'status': future_status})
    future = self.executor.futures.pop(task_id)
    return jsonify({'status': 'done', 'result': future.result()})

My problem is that whenever I call task_status method i got TypeError: 'str' object is not callable. I found that future_result returns <bound method Future.result of <Future at 0x219fed6f510 state=finished raised TypeError>>. Thanks in advance for any hints on the error.


Solution

  • In scan_and_collect_async you submit a return value of scan_and_collect to executor. In your case the return value is a string.

    Executor expects a callable to run, optionally followed by args and kwargs to pass to that callable.

    So, the correct implementation would be

    def scan_and_collect_async(self):
        task_id = uuid.uuid4().hex
        self.executor.submit_stored(task_id, self.scan_and_collect)
        return jsonify({'task_id': task_id})