How to access print output from background threads in jupyter notebook

Viewed 425

[Update] It seems this is working again as expected in JupyterLab (tested on macos and jupyterlab 3.1.13).

Some while ago, I could see the print output of a background thread in the active cell in jupyter notebook. I know this sometimes led to somewhat strange behaviour if you had an active background thread while working on in your notebook. But it came in handy for debugging.

Meanwhile, background thread printing to active cells only works while the code of the active cell is executing (see Threading in Jupyter notebook).

Simple example:

import threading

def run():
    
    time.sleep(2)
    print('a')
    time.sleep(1)
    print('b')
    
t = threading.Thread(target=run)
t.start()

time.sleep(4)  # <-- I don't want to have this

What I actually want to do is to debug a pyqt5 widget, using %gui qt5, which means that I can't block the main thread with a sleep as I did in the example above. But then, all the prints get lost.

So my question is: Can I access the print output that happened in a background thread while no code cell has been executing?

Thanks!

0 Answers
Related