如何检查我的Bokeh服务器应用程序是否已完全加载和呈现?

时间:2017-03-14 19:07:24

标签: javascript python node.js electron bokeh

我想将我的Bokeh服务器应用程序集成到Electron中。所以我做的是使用python-shell像这样运行散景服务器

mainWindow = new BrowserWindow({
    width: 1000,
    height: 700,
    show: false,
})

var PythonShell = require('python-shell');
var options = {
    mode: 'text',
    pythonPath: 'python3',
    pythonOptions: ['-m'],
    scriptPath: '',
    args: ['serve','bokeh_project/']
};

PythonShell.run('bokeh', options, function (err, results) {
    if (err) throw err;
    console.log('results: %j', results);
});

mainWindow.loadURL('http://localhost:5006');

mainWindow.once('did-finish-load', () => {
    mainWindow.show()
})

这里的问题是窗口永远不会弹出,因为电子没有检测到服务器已加载。

1 个答案:

答案 0 :(得分:0)

我发现了很多解决方法。我仍在等待最终解决方案。

解决方法1

因此,我必须添加此setTimeout作为解决方法。如果我不使用此页面,该页面将永远卡住。

setTimeout(function () {
    mainWindow.show();
    mainWindow.loadURL('http://localhost:5006');
}, 3000);

解决方法2

它检查散景端口是否仍然关闭。但是元素可能未加载且未完全加载

var portscanner = require('portscanner')
var _checkServerStatus = setInterval(function() {
  portscanner.checkPortStatus(5006, '127.0.0.1', function(error, status) {
    if (status == 'open') {  // status = 'open' or 'close'
        clearInterval(_checkServerStatus);
        console.log('Server running');
        mainWindow.loadURL(bokehUrl); 
    }
  });
}, 100);  

解决方法3

最后,我找到了另一个解决方法,以检查所有元素是否都已完全呈现。答案在this question中:

oldLog = console.log;
console.log = function (message) {
    if(message.localeCompare('Bokeh items were rendered successfully') == 0){
        window.top.postMessage('show-bokeh-iframe', '*')
        console.log = oldLog;
    }
    oldLog.apply(console, arguments);
};

解决方法4

有一个GH issue,当bokeh完全加载并呈现时,作者要求在其中调用回调。用户foobarbecue建议验证bokeh页面是否使用MutationObserver呈现,但我从未使用过。