Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

json mode and run method #16

Closed
Olmer1st opened this issue Oct 11, 2015 · 5 comments
Closed

json mode and run method #16

Olmer1st opened this issue Oct 11, 2015 · 5 comments

Comments

@Olmer1st
Copy link

Is it possible to use PythonShell.run together with mode:JSON?
What is correct options structure?

@extrabacon
Copy link
Owner

Simply set options using the second argument, like so:

PythonShell.run('script.py', { mode: 'json' });

@Olmer1st
Copy link
Author

Yes. Ok. But how to transfer message?

@Olmer1st Olmer1st reopened this Oct 13, 2015
@extrabacon
Copy link
Owner

Can you clarify your question? What are you struggling with exactly?

Basically, run will return an instance which you can use to send and receive messages.

var python = PythonShell.run('script.py', { mode: 'json' });
// send a message
python.send({ hello: 'world' });
// receive a message from Python
python.on('message', function (message) {
  // handle message sent from Python
}

On the Python side, you simply read from stdin and write to stdout.

# send a JSON message to node
print json.dumps(obj)
# receive messages
for message in sys.stdin:
  # handle message sent from node

@Olmer1st
Copy link
Author

I want to use this example:
PythonShell.run('my_script.py', options, function (err, results) {
if (err) throw err;
// results is an array consisting of messages collected during execution
console.log('results: %j', results);
});

this example work only with args.

@Almenon
Copy link
Collaborator

Almenon commented Mar 3, 2018

Still having trouble? If not I'll close this.

@Almenon Almenon closed this as completed Mar 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants