Skip to content

python stdin buffering issue #18

@punit-dato

Description

@punit-dato

Hey - I'm trying to the send/message functionality on python-shell to exchange data between a long-running python script and node, but I'm seeing that the messages don't get handled till I end the input stream. For example, the following sample code does not invoke the 'message' callback (or 'close' or 'error):

var pyshell = new PythonShell('python/helloworld.py', options);

pyshell.send({'hello': 'world'});

pyshell.on('message', function(message) {
  console.log("message - ", message);
}).on('close', function (result) {
  console.log("close - ", result);
}).on('error', function (err) {
  console.log("error")
});

The python is the same as your echo script.

import sys, json

# simple JSON echo script
for line in sys.stdin:
  print json.dumps(json.loads(line))

Now, if I close the connection after the above code, then I get all the messages:

pyshell.end(function (err) {
  if (err) throw err;
  console.log('finished');
});

This won't work for me as my python script maintains state and I don't want to re-run the script every time. I'd rather have a long running child process I could pass messages back and forth from. This seems like a bug. Any suggestions?

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