1

I'm writing a library for use with Jupyter notebook. One of the things it does is asynchronously update progress in the notebook cell output. In the library I have a function:

def request_result():
    start_result_calculation()  # starts a calculation and returns immediately
    asyncio.get_event_loop().create_task(report_progress())
    result = wait_for_result()  # waits for the calc to complete and returns the result
    return result

If I now call result = request_result() in a Jupyter notebook cell, the report_progress() coroutine is never invoked. On the other hand, If I return the Task back to the cell:

def calculate_result():
    start_result_calculation()
    return asyncio.get_event_loop().create_task(report_progress())

then when calculate_result() is executed in a notebook cell the report_progress() is invoked. Why is that, and is there a way to have the first function work as expected, spawning a background task and returning the final result?

narthi
  • 2,188
  • 1
  • 16
  • 27

1 Answers1

0

One work-around that works in my case is to run the task from a separate thread:

class _ProgressThread(threading.Thread):
   def run(self):
      asyncio.run(report_progress())


def request_result():
    start_result_calculation()
    _ProgressThread().start()
    result = wait_for_result()
    return result

I still need to use asyncio in that thread, due to the requirements of the code that runs inside report_progress().

narthi
  • 2,188
  • 1
  • 16
  • 27