Skip to content

Dynamically parsing output #78

Description

@jonesbra

I used the functionality shell.on('message', function(message){ console.log(message) }), and I am able to collect all output of my script, and log it AFTER the script has finished. I would like to parse output as it is printed (dynamically) so that I may implement a progress bar in my electron app. Is this possible with python-shell?

Activity

  1. brucedjones commented on Apr 6, 2017

    @brucedjones
    Collaborator

    I have noticed the same behaviour.

    I found that you need to flush stdout from your python script to get the message event to fire. See this SO answer and comments for more information.

    Some solutions are given here. However, I have tested both the -u option and the PYTHONUNBUFFERED environment variable (on windows) without success.

    I have found two workarounds which work with python-shell on windows, though neither are ideal.

    1. Call sys.stdout.flush() whenever you want to fire the 'message' event
    2. preppend the code in this answer to my script before passing it to python-shell
  2. MagnusPoppe commented on Feb 5, 2018

    @MagnusPoppe

    This is actually not an error. Just you not setting the "pythonOptions: ['-u']," flag before running the script.

          new PythonShell(scriptPath, {
            encoding: 'utf8',
            pythonPath: path.normalize(config().python),
            pythonOptions: ['-u'], // THIS ONE!!! 
            args:  args
          })
  3. brucedjones commented on Feb 5, 2018

    @brucedjones
    Collaborator

    @MagnusPoppe, please actually read my previous comment.

  4. Almenon commented on Feb 14, 2018

    @Almenon
    Collaborator

    The -u worked for me w/ python 3.6.0 on windows 10. What version of python are you using @brucedjones ?

    see Almenon/AREPL-backend#6

  5. Almenon commented on Jun 25, 2018

    @Almenon
    Collaborator

    no response from anyone in the thread for a couple months. Closing.

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions