我正在设计一个Kafka解决方案。最后,我创建了一个带有破折号和绘图的仪表板。通常,我可以在图表上解析后显示数据。但是,当我从另一个在线程内运行的函数中获取数据时,该图不会绘制任何内容。
我使用Python 3.5
正常代码有效。我获取数据,解析时间戳(13:20:15-13:20:55)和消息计数。图总是根据时间间隔重新加载。
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly
from dash.dependencies import Input, Output
TIME_INTERVAL = 100
veri = [[['2019-04-09 13:11:25'], ['INFO'], ['Istanbul'], ['Hello-From-Istanbul']], [['2019-04-09 13:11:26'], ['FATAL'], ['London'], ['Hello-From-London']], [['2019-04-09 13:11:27'], ['FATAL'], ['Istanbul'], ['Hello-From-Istanbul']], [['2019-04-09 13:11:28'], ['INFO'], ['Moskow'], ['Hello-From-Moskow']], [['2019-04-09 13:11:29'], ['INFO'], ['Moskow'], ['Hello-From-Moskow']], [['2019-04-09 13:11:30'], ['FATAL'], ['Tokyo'], ['Hello-From-Tokyo']], [['2019-04-09 13:11:31'], ['DEBUG'], ['Istanbul'], ['Hello-From-Istanbul']], [['2019-04-09 13:11:32'], ['FATAL'], ['Beijing'], ['Hello-From-Beijing']] ..... ]
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div(
html.Div([
html.H4('Kafka Dashboard'),
dcc.Graph(id='live-update-graph'),
dcc.Interval(
id='interval-component',
interval=1 *1000,
n_intervals=0
)
])
)
countofmessage=[]
timeseries=[]
for i in range(len(veri)):
times = veri[i][0][0].split(" ")[1]
timeseries.append(times)
if len(veri) % TIME_INTERVAL == 0:
rest = 0
next = 0
else:
rest = len(veri) % TIME_INTERVAL
next = 1
for i in range(0, int(len(veri)/TIME_INTERVAL)+next):
countofmessage.append([0, 0, 0, 0, 0])
for i in range(0, int(len(veri)/TIME_INTERVAL)+next):
d = i * TIME_INTERVAL
if i == int(len(veri)/TIME_INTERVAL):
plus = rest
else:
plus = TIME_INTERVAL
for a in range(d, d+plus):
if veri[a][2][0] == "Moskow":
countofmessage[i][0] += 1
if veri[a][2][0] == "Istanbul":
countofmessage[i][1] += 1
if veri[a][2][0] == "Tokyo":
countofmessage[i][2] += 1
if veri[a][2][0] == "Beijing":
countofmessage[i][3] += 1
if veri[a][2][0] == "London":
countofmessage[i][4] += 1
@app.callback(Output('live-update-graph', 'figure'),
[Input('interval-component', 'n_intervals')])
def update_graph_live(n) :
data = {
'moskow_x': [],
'moskow_y': [],
'istanbul_x': [],
'istanbul_y': [],
'tokyo_x': [],
'tokyo_y': [],
'beijing_x': [],
'beijing_y': [],
'london_x': [],
'london_y': [],
}
# Collect some data
for i in range(0, int(len(veri)/TIME_INTERVAL)+next):
data['moskow_x'].append(timeseries[i*TIME_INTERVAL])
data['istanbul_x'].append(timeseries[i * TIME_INTERVAL])
data['tokyo_x'].append(timeseries[i * TIME_INTERVAL])
data['beijing_x'].append(timeseries[i * TIME_INTERVAL])
data['london_x'].append(timeseries[i * TIME_INTERVAL])
for i in range(len(countofmessage)) :
data['moskow_y'].append(countofmessage[i][0])
data['istanbul_y'].append(countofmessage[i][1])
data['tokyo_y'].append(countofmessage[i][2])
data['beijing_y'].append(countofmessage[i][3])
data['london_y'].append(countofmessage[i][4])
fig = plotly.tools.make_subplots(rows=1, cols=1, vertical_spacing=0.2)
fig['layout']['margin'] = {
'l' : 30, 'r' : 10, 'b' : 30, 't' : 10
}
fig['layout']['legend'] = {'x' : 0, 'y' : 1, 'xanchor' : 'left'}
fig.append_trace({
'x' : data['moskow_x'],
'y' : data['moskow_y'],
'name' : 'Moskow',
'mode' : 'lines+markers',
'type' : 'scatter'
}, 1, 1)
fig.append_trace({
'x' : data['istanbul_x'],
'y' : data['istanbul_y'],
'name' : 'Istanbul',
'mode' : 'lines+markers',
'type' : 'scatter'
}, 1, 1)
fig.append_trace({
'x' : data['tokyo_x'],
'y' : data['tokyo_y'],
'name' : 'Tokyo',
'mode' : 'lines+markers',
'type' : 'scatter'
}, 1, 1)
fig.append_trace({
'x' : data['beijing_x'],
'y' : data['beijing_y'],
'name' : 'Beijing',
'mode' : 'lines+markers',
'type' : 'scatter'
}, 1, 1)
fig.append_trace({
'x' : data['london_x'],
'y' : data['london_y'],
'name' : 'London',
'mode' : 'lines+markers',
'type' : 'scatter'
}, 1, 1)
return fig
app.run_server()
我从kafka获取数据。然后在getdata()中解析。然后用图形查看值,但什么也没画。 listen()getdata()和update_graph同时工作。
from confluent_kafka import Consumer
from pymongo import MongoClient
import threading
import datetime
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly
from dash.dependencies import Input, Output
from multiprocessing import Process
veri = []
TIME_INTERVAL=30
next=0
oldveri = 0
timeseries=[['13:11:25']]
countofmessage=[]
splitted = []
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div(
html.Div([
html.H4('Kafka Dashboard'),
dcc.Graph(id='live-update-graph'),
dcc.Interval(
id='interval-component',
interval=1 * 1000,
n_intervals=0
)
])
)
client = MongoClient("localhost", 27017)
db = client.get_database("people")
messages = db.get_collection("messages")
c = Consumer({
'bootstrap.servers': 'localhost:9092',
'group.id': 'my-group',
'auto.offset.reset': 'earliest'
})
c.subscribe(['my-topic'])
def listen():
while True :
msg = c.poll(1.0)
if msg is None :
continue
if msg.error() :
print("Consumer error: {}".format(msg.error()))
continue
splitted = msg.value().decode('utf-8').split(" ")
veri.append([[splitted[0] + " " + splitted[1]], [splitted[2]], [splitted[3]], [splitted[4]]])
c.close()
def getdata():
while True:
if oldveri < len(veri):
global oldveri
oldveri = len(veri)
for i in range(len(veri)):
times = veri[i][0][0].split(" ")[1]
timeseries.append(times)
if len(veri) % TIME_INTERVAL == 0 :
rest = 0
next = 0
else :
rest = len(veri) % TIME_INTERVAL
next = 1
for i in range(0, int(len(veri) / TIME_INTERVAL) + next) :
countofmessage.append([0, 0, 0, 0, 0])
for i in range(0, int(len(veri) / TIME_INTERVAL) + next) :
a = i * TIME_INTERVAL
if i == int(len(veri) / TIME_INTERVAL) :
plus = rest
else :
plus = TIME_INTERVAL
for a in range(a, a + plus) :
if veri[a][2][0] == "Moskow" :
countofmessage[i][0] += 1
if veri[a][2][0] == "Istanbul" :
countofmessage[i][1] += 1
if veri[a][2][0] == "Tokyo" :
countofmessage[i][2] += 1
if veri[a][2][0] == "Beijing" :
countofmessage[i][3] += 1
if veri[a][2][0] == "London" :
countofmessage[i][4] += 1
@app.callback(Output('live-update-graph', 'figure'),
[Input('interval-component', 'n_intervals')])
def update_graph_live(n):
data = {
'moskow_x': [],
'moskow_y': [],
'istanbul_x': [],
'istanbul_y': [],
'tokyo_x': [],
'tokyo_y': [],
'beijing_x': [],
'beijing_y': [],
'london_x': [],
'london_y': [],
}
# Collect some data
for i in range(0, int(len(veri)/30)+next+1):
data['moskow_x'].append(timeseries[i*30])
data['istanbul_x'].append(timeseries[i*30])
data['tokyo_x'].append(timeseries[i*30])
data['beijing_x'].append(timeseries[i*30])
data['london_x'].append(timeseries[i*30])
for i in range(len(countofmessage)):
data['moskow_y'].append(countofmessage[i][0])
data['istanbul_y'].append(countofmessage[i][1])
data['tokyo_y'].append(countofmessage[i][2])
data['beijing_y'].append(countofmessage[i][3])
data['london_y'].append(countofmessage[i][4])
fig = plotly.tools.make_subplots(rows=1, cols=1, vertical_spacing=0.2)
fig['layout']['margin'] = {
'l': 30, 'r': 10, 'b': 30, 't': 10
}
fig['layout']['legend'] = {'x': 0, 'y': 1, 'xanchor': 'left'}
fig.append_trace({
'x': data['moskow_x'],
'y': data['moskow_y'],
'name': 'Moskow',
'mode': 'lines+markers',
'type': 'scatter'
}, 1, 1)
fig.append_trace({
'x': data['istanbul_x'],
'y': data['istanbul_y'],
'name': 'Istanbul',
'mode': 'lines+markers',
'type': 'scatter'
}, 1, 1)
fig.append_trace({
'x': data['tokyo_x'],
'y': data['tokyo_y'],
'name': 'Tokyo',
'mode': 'lines+markers',
'type': 'scatter'
}, 1, 1)
fig.append_trace({
'x': data['beijing_x'],
'y': data['beijing_y'],
'name': 'Beijing',
'mode': 'lines+marTIME_INTERVALkers',
'type': 'scatter'
}, 1, 1)
fig.append_trace({
'x': data['london_x'],
'y': data['london_y'],
'name': 'London',
'mode': 'lines+markers',
'type': 'scatter'
}, 1, 1)
return fig
def startserver():
app.run_server()
sv = threading.Thread(name='startserver', target=startserver)
tb = threading.Thread(name='getdata', target=getdata)
ta = threading.Thread(name='listen', target=listen)
sv.start()
tb.start()
ta.start()
我该如何解决?