我正在尝试使用破折号来实时监视JBeans属性。使用dcc.Interval时,第一次尝试POST / _dash-update-component组件后,程序会自动关闭。
我已连接到Jconsole。引用Jbean属性并提取值是可行的。甚至在我第一次启动仪表板应用程序时有效,但是随后触发间隔时,仪表板程序关闭。
步骤:
import jpype as jpype
from jpype import java
from jpype import javax
import sys, os
import pandas as pd
import datetime
#JMX CONNECTION FUNCTION:
def jmx_connect(HOST, USER, PORT, PASS):
URL = 'service:jmx:rmi:///jndi/rmi://'+HOST+':'+PORT+'/jmxrmi'
jpype.startJVM(jpype.get_default_jvm_path())
jhash = java.util.HashMap()
jarray=jpype.JArray(java.lang.String)([USER,PASS])
jhash.put(javax.management.remote.JMXConnector.CREDENTIALS, jarray);
jmxurl = javax.management.remote.JMXServiceURL(URL)
jmxsoc = javax.management.remote.JMXConnectorFactory.connect(jmxurl,jhash)
connection = jmxsoc.getMBeanServerConnection();
return connection
#Enter login details HERE:
HOST =
USER =
PORT =
PASS =
#initializing connection with my login details
connection = jmx_connect(HOST, USER, PORT, PASS)
#Thread Count function to extract number of active threads:
#THREAD COUNT
def jmx_ThreadCount():
object="java.lang:type=Threading"
attribute= "ThreadCount"
attr = connection.getAttribute(javax.management.ObjectName(object),attribute)
return attr
from TEST_jvmRun import * #this is import of my JVM functions
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.graph_objs as go
app = dash.Dash()
app.layout = html.Div([
html.H1(id='live-update-text'),
dcc.Interval(id='interval-component', interval=10000, n_intervals=0)
])
@app.callback(Output('live-update-text', 'children'),
[Input('interval-component', 'n_intervals')])
def update_current_delay(n):
#return f"counter: {n}"
return f"WORKING, trial: {n} ; {jmx_ThreadCount()}"
if __name__ == '__main__':
app.run_server()
a)实际结果
---命令行输出:
C:\Users\m011472\Documents\Python Scripts\Web>cd "c:\Users\m011472\Documents\Python Scripts\Web" && cmd /C "set "PYTHONIOENCODING=UTF-8" && set "PYTHONUNBUFFERED=1" && C:\Python36\python.exe C:\Users\m011472\.vscode\extensions\ms-python.python-2018.4.0\pythonFiles\PythonTools\visualstudio_py_launcher.py "c:\Users\m011472\Documents\Python Scripts\Web" 53564 34806ad9-833a-4524-8cd6-18ca4aa74f14 RedirectOutput,RedirectOutput "c:\Users\m011472\Documents\Python Scripts\Web\TEST_dashboard.py" "
* Serving Flask app "TEST_dashboard" (lazy loading) * Environment: production WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:8050/ (Press CTRL+C to quit)
127.0.0.1 - - [28/Mar/2019 14:57:02] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [28/Mar/2019 14:57:03] "GET /_dash-layout HTTP/1.1" 200 -
127.0.0.1 - - [28/Mar/2019 14:57:03] "GET /_dash-dependencies HTTP/1.1" 200 -
127.0.0.1 - - [28/Mar/2019 14:57:03] "POST /_dash-update-component HTTP/1.1" 200 -
C:\Users\m011472\Documents\Python Scripts\Web>
---仪表板输出:
工作,试用:0; 418
b)预期结果
---命令行输出:
程序应每10秒执行一次POST,并且永不结束
---仪表板输出:
数字应每10秒更新一次
工作,试用:0; XXX(任何数字) 工作,试用:1; XXY 工作,试用:2; XXZ
答案 0 :(得分:0)
应该有人感兴趣-我找到了部分解决方案。 dcc.Interval引起的问题,因为它在每个间隔创建一个新线程,并且JMX调用要求将JVM实例附加到当前线程。
def jmx_ThreadCount():
print("State=", jpype.isThreadAttachedToJVM())
if not jpype.isThreadAttachedToJVM():
print ("Needs to attach...")
jpype.attachThreadToJVM()
print ("Check Attached=", jpype.isThreadAttachedToJVM())
object="java.lang:type=Threading"
attribute= "ThreadCount"
attr = connection.getAttribute(javax.management.ObjectName(object),attribute)
return attr
现在的问题是:如何防止dcc.Interval在每个间隔创建新线程?