-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblocking_computation.py
More file actions
34 lines (25 loc) · 845 Bytes
/
blocking_computation.py
File metadata and controls
34 lines (25 loc) · 845 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import time
from functools import partial
from random import random
from threading import Thread
from bokeh.models import ColumnDataSource
from bokeh.plotting import curdoc, figure
# only modify from a Bokeh session callback
source = ColumnDataSource(data=dict(x=[0], y=[0]))
# This is important! Save curdoc() to make sure all threads
# see the same document.
doc = curdoc()
async def update(x, y):
source.stream(dict(x=[x], y=[y]))
def blocking_task():
while True:
# do some blocking computation
time.sleep(0.1)
x, y = random(), random()
# but update the document from a callback
doc.add_next_tick_callback(partial(update, x=x, y=y))
p = figure(x_range=[0, 1], y_range=[0,1])
l = p.circle(x='x', y='y', source=source)
doc.add_root(p)
thread = Thread(target=blocking_task)
thread.start()