Skip to content

json mode and run method #16

Closed
Closed

Description

@Olmer1st

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

Activity

  1. extrabacon commented on Oct 13, 2015

    @extrabacon
    Owner

    Simply set options using the second argument, like so:

    PythonShell.run('script.py', { mode: 'json' });
  2. Olmer1st commented on Oct 13, 2015

    @Olmer1st
    Author

    Yes. Ok. But how to transfer message?

  3. extrabacon commented on Oct 13, 2015

    @extrabacon
    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
  4. Olmer1st commented on Oct 13, 2015

    @Olmer1st
    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.

  5. Almenon commented on Mar 3, 2018

    @Almenon
    Collaborator

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

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