Skip to content

Instantly share code, notes, and snippets.

@tikikun
Forked from maartenbreddels/ipython_thread.py
Created April 16, 2020 23:13
Show Gist options
  • Save tikikun/2792d7fc82567bc9fd605e8696839899 to your computer and use it in GitHub Desktop.
Save tikikun/2792d7fc82567bc9fd605e8696839899 to your computer and use it in GitHub Desktop.
IPython snippet for doing work in a thread, and updating a progressbar
import threading
from IPython.display import display
import ipywidgets as widgets
import time
def get_ioloop():
import IPython, zmq
ipython = IPython.get_ipython()
if ipython and hasattr(ipython, 'kernel'):
return zmq.eventloop.ioloop.IOLoop.instance()
ioloop = get_ioloop()
thread_safe = True
def work():
for i in range(10):
def update_progress(i=i):
print "calling from thread", threading.currentThread()
progress.value = (i+1)/10.
print i
time.sleep(0.5)
if thread_safe:
get_ioloop().add_callback(update_progress)
else:
update_progress()
print "we are in thread", threading.currentThread()
thread = threading.Thread(target=work)
progress = widgets.FloatProgress(value=0.0, min=0.0, max=1.0, step=0.01)
display(progress)
thread.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment