Skip to content

How to call a python child process as a server and redirect output in real-time? #103

Description

@chesucr

I would like to run the bokeh server as a child process with Node. I would like to use this package and redirect the output in real-time, not at the end of the execution.

Bokeh does not have a configuration file or argument where I can write the logger file path. So I need to use command redirection operators in order to reroute the logger:

'>> `${debug_path}` 2>&1'

So I have tried some things:

process.chdir(`${path}`);  // path where I should run bokeh
var options = {
mode: 'json',
pythonPath: `${python_path}`,
pythonOptions: [
        '-m', 'bokeh', 'serve',
    ],
    args: [
        '>>', `${debug_path}`, '2>&1',   // this is not working, in pythonOptions neither
    ]
};
python_shell.run('', options, function (err, results) {
    if (err) {
        logger.error(`${err}`);
    }
    // results is an array consisting of messages collected during execution
    if (typeof(results) !== 'undefined') {
        logger.info(results);  // this is run at the end of the execution if it was an script
    }
});

However I can do it with exec method. But it has some drawbacks, if the node process is killed by forced, the python process become a zombie process. So this is not a good solution:

// command is a string with the whole paths and arguments concatenating '>> `${debug_path}` 2>&1' at the end
shell = child_process.exec(command, (error, stdout, stderr) => {
    if (error) {
        logger.error(`${error}`);
    }
    logger.info(`stdout: ${stdout}`);
    logger.info(`stderr: ${stderr}`);
});

Is this possible? I will appreciate any help or piece of advice

Activity

  1. extrabacon commented on Jan 18, 2018

    @extrabacon
    Owner
  2. Almenon commented on Jan 22, 2018

    @Almenon
    Collaborator

    I was wondering why my print results were not in real-time. Thanks @extrabacon

  3. chesucr commented on Jan 22, 2018

    @chesucr
    Author

    @extrabacon thanks for your attention

    Yes, I am afraid the bokeh logger is not under my control. Only if I could write directly into a file the problem would be solved. python_shell takes the info from the stdout if I use the print python function. But, what is happening with the information printed with the bokeh logger or the python logger by default? It just disappears? Imagine this simple python script

    import logging
    import sys
    
    logger = logging.getLogger()
    logger.info('before redirect stream')
    
    logging.basicConfig(stream=sys.stdout, level=logging.INFO)
    logger2 = logging.getLogger()
    logger2.info('python logger message')
    print('print content')
    

    The stdout object takes the last both messages well. But it is not taking the first one, is it vanished? I am wondering if there is a way to redirect the bokeh logger stream.

  4. chesucr commented on Jan 23, 2018

    @chesucr
    Author

    Well, anyway I think this shouldn´t be a python_shell issue. So I made a request to change the logging output in the bokeh repository

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions