Trent Fowler
Trent Fowler

Reputation: 103

Displaying message during lengthy cell execution

I'm working on a jupyter notebook in which one cell takes a while to produce a series of graphs. To reassure users that the platform isn't malfunctioning and the cell isn't frozen I'd like to do something like print('computing') every 5 seconds until the cell finishes executing.

Is there a straightforward way to do this in the jupyter notebook environment? I've explored some of the native timing functionality and it doesn't appear as though there's anything that quite does this.

Upvotes: 2

Views: 150

Answers (1)

MyNameIsCaleb
MyNameIsCaleb

Reputation: 4489

You can use threading or multiprocessing to do this, I used threading below.

import time
from threading import Thread

def progress(stop):
    while True:
        print('Cell Running...')
        time.sleep(5)
        if stop():
            break

stop_threads = False
t1 = Thread(target=progress, args=(lambda: stop_threads, ))
t1.start()

# do main here
print('from main')
time.sleep(6)
print('from main 2')
stop_threads = True

# join your thread
t1.join()

Upvotes: 2

Related Questions