You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+14-7Lines changed: 14 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,8 @@ A simple way to run Python scripts from Node.js with basic but efficient inter-p
5
5
## Features
6
6
7
7
+ Reliably spawn Python scripts in a child process
8
-
+ Text, JSON and binary modes
8
+
+ Built-in text, JSON and binary modes
9
+
+ Custom parsers and formatters
9
10
+ Simple and efficient data transfers through stdin and stdout streams
10
11
+ Extended stack traces when an error is thrown
11
12
@@ -78,6 +79,12 @@ pyshell.end(function (err) {
78
79
79
80
Use `.send(message)` to send a message to the Python script. Attach the `message` event to listen to messages emitted from the Python script.
80
81
82
+
Use `options.mode` to quickly setup how data is sent and received between your Node and Python applications.
83
+
84
+
* use `text` mode for exchanging lines of text
85
+
* use `json` mode for exchanging JSON fragments
86
+
* use `binary` mode for anything else (data is sent and received as-is)
87
+
81
88
For more details and examples including Python source code, take a look at the tests.
82
89
83
90
### Error Handling and extended stack traces
@@ -129,7 +136,9 @@ Creates an instance of `PythonShell` and starts the Python process
129
136
*`text`: each line of data (ending with "\n") is emitted as a message (default)
130
137
*`json`: each line of data (ending with "\n") is parsed as JSON and emitted as a message
131
138
*`binary`: data is streamed as-is through `stdout` and `stdin`
132
-
*`parser`: each line of data (ending with "\n") is parsed with this customer parser (overrides `mode`)
139
+
*`formatter`: each message to send is transformed using this method, then appended with "\n"
140
+
*`parser`: each line of data (ending with "\n") is parsed with this function and its result is emitted as a message
141
+
*`encoding`: the text encoding to apply on the child process streams (default: "utf8")
133
142
*`pythonPath`: The path where to locate the "python" executable. Default: "python"
134
143
*`pythonOptions`: Array of option switches to pass to "python"
135
144
*`scriptPath`: The default path where to look for scripts. Default: "./python"
@@ -179,9 +188,7 @@ PythonShell.run('script.py', function (err, results) {
179
188
180
189
#### `.send(message)`
181
190
182
-
Sends a message to the Python script via stdin. The data is formatted according to the selected mode (text or JSON). This method can be overridden in order to format the data in some other way.
183
-
184
-
This method should not be used in binary mode.
191
+
Sends a message to the Python script via stdin. The data is formatted according to the selected mode (text or JSON), or through a custom function when `formatter` is specified.
Parses incoming data from the Python script written via stdout and emits `message` events. The data is parsed as JSON if mode has been set to "json". This method is called automatically as data is being received from stdout and can be overridden to parse the data differently.
206
+
Parses incoming data from the Python script written via stdout and emits `message` events. This method is called automatically as data is being received from stdout.
200
207
201
208
#### `.end(callback)`
202
209
203
210
Closes the stdin stream, allowing the Python script to finish and exit. The optional callback is invoked when the process is terminated.
204
211
205
212
#### event: `message`
206
213
207
-
Fires when a chunk of data is parsed from the stdout stream via the `receive` method. This event is not emitted in binary mode.
214
+
Fires when a chunk of data is parsed from the stdout stream via the `receive` method. If a `parser` method is specified, the result of this function will be the message value. This event is not emitted in binary mode.
0 commit comments