为什么node.js打印PythonShell变量?

时间:2018-11-28 01:44:42

标签: python node.js

首先让我说我是Node.js的新手,我一直在研究如何使用Node.js中的Python,遇到了称为“ Python-Shell”的Node.js模块

Python-Shell Page

下面列出的页面上提供的 runString 示例是在将“ 完成”打印到控制台之前打印变量PythonShell。

let {PythonShell} = require('python-shell')

PythonShell.runString('x=1+1;print(x)', null, function (err) {
  if (err) throw err;
  console.log('finished');
});

请参见下面的长结果

我可以避免吗?

谢谢

> let {PythonShell} = require('python-shell')
undefined
> 
> PythonShell.runString('x=1+1;print(x)', null, function (err) {
...   if (err) throw err;
...   console.log('finished');
... });
PythonShell {
  domain:
   Domain {
     domain: null,
     _events:
      [Object: null prototype] {
        removeListener: [Function: updateExceptionCapture],
        newListener: [Function: updateExceptionCapture],
        error: [Function: debugDomainError] },
     _eventsCount: 3,
     _maxListeners: undefined,
     members: [] },
  _events: [Object: null prototype] { message: [Function] },
  _eventsCount: 1,
  _maxListeners: undefined,
  scriptPath:
   '/var/folders/f7/kfjr8m7d1d3ggyj1jj90jjqm0000gn/T/pythonShellFile5420121840.py',
  command:
   [ '/var/folders/f7/kfjr8m7d1d3ggyj1jj90jjqm0000gn/T/pythonShellFile5420121840.py' ],
  mode: 'text',
  formatter: [Function: toText],
  parser: [Function: asText],
  stderrParser: [Function: asText],
  terminated: false,
  childProcess:
   ChildProcess {
     domain:
      Domain {
        domain: null,
        _events: [Object],
        _eventsCount: 3,
        _maxListeners: undefined,
        members: [] },
     _events: [Object: null prototype] { exit: [Function] },
     _eventsCount: 1,
     _maxListeners: undefined,
     _closesNeeded: 3,
     _closesGot: 0,
     connected: false,
     signalCode: null,
     exitCode: null,
     killed: false,
     spawnfile: 'python3',
     _handle:
      Process {
        domain: [Domain],
        onexit: [Function],
        pid: 2420,
        [Symbol(owner)]: [Circular] },
     spawnargs:
      [ 'python3',
        '/var/folders/f7/kfjr8m7d1d3ggyj1jj90jjqm0000gn/T/pythonShellFile5420121840.py' ],
     pid: 2420,
     stdin:
      Socket {
        connecting: false,
        _hadError: false,
        _handle: [Pipe],
        _parent: null,
        _host: null,
        _readableState: [ReadableState],
        readable: false,
        domain: [Domain],
        _events: [Object],
        _eventsCount: 1,
        _maxListeners: undefined,
        _writableState: [WritableState],
        writable: false,
        allowHalfOpen: false,
        _sockname: null,
        _pendingData: null,
        _pendingEncoding: '',
        server: null,
        _server: null,
        [Symbol(asyncId)]: 217,
        [Symbol(lastWriteQueueSize)]: 0,
        [Symbol(timeout)]: null,
        [Symbol(kBytesRead)]: 0,
        [Symbol(kBytesWritten)]: 0 },
     stdout:
      Socket {
        connecting: false,
        _hadError: false,
        _handle: [Pipe],
        _parent: null,
        _host: null,
        _readableState: [ReadableState],
        readable: true,
        domain: [Domain],
        _events: [Object],
        _eventsCount: 3,
        _maxListeners: undefined,
        _writableState: [WritableState],
        writable: false,
        allowHalfOpen: false,
        _sockname: null,
        _pendingData: null,
        _pendingEncoding: '',
        server: null,
        _server: null,
        [Symbol(asyncId)]: 218,
        [Symbol(lastWriteQueueSize)]: 0,
        [Symbol(timeout)]: null,
        [Symbol(kBytesRead)]: 0,
        [Symbol(kBytesWritten)]: 0 },
     stderr:
      Socket {
        connecting: false,
        _hadError: false,
        _handle: [Pipe],
        _parent: null,
        _host: null,
        _readableState: [ReadableState],
        readable: true,
        domain: [Domain],
        _events: [Object],
        _eventsCount: 3,
        _maxListeners: undefined,
        _writableState: [WritableState],
        writable: false,
        allowHalfOpen: false,
        _sockname: null,
        _pendingData: null,
        _pendingEncoding: '',
        server: null,
        _server: null,
        [Symbol(asyncId)]: 219,
        [Symbol(lastWriteQueueSize)]: 0,
        [Symbol(timeout)]: null,
        [Symbol(kBytesRead)]: 0,
        [Symbol(kBytesWritten)]: 0 },
     stdio: [ [Socket], [Socket], [Socket] ] },
  stdout:
   Socket {
     connecting: false,
     _hadError: false,
     _handle:
      Pipe {
        domain: [Domain],
        onread: [Function: onStreamRead],
        reading: true,
        [Symbol(owner)]: [Circular] },
     _parent: null,
     _host: null,
     _readableState:
      ReadableState {
        objectMode: false,
        highWaterMark: 16384,
        buffer: BufferList { head: null, tail: null, length: 0 },
        length: 0,
        pipes: null,
        pipesCount: 0,
        flowing: true,
        ended: false,
        endEmitted: false,
        reading: true,
        sync: false,
        needReadable: true,
        emittedReadable: false,
        readableListening: false,
        resumeScheduled: true,
        emitClose: false,
        autoDestroy: false,
        destroyed: false,
        defaultEncoding: 'utf8',
        awaitDrain: 0,
        readingMore: false,
        decoder: [StringDecoder],
        encoding: 'utf8' },
     readable: true,
     domain:
      Domain {
        domain: null,
        _events: [Object],
        _eventsCount: 3,
        _maxListeners: undefined,
        members: [] },
     _events:
      [Object: null prototype] {
        end: [Array],
        close: [Function],
        data: [Function: bound receive] },
     _eventsCount: 3,
     _maxListeners: undefined,
     _writableState:
      WritableState {
        objectMode: false,
        highWaterMark: 16384,
        finalCalled: false,
        needDrain: false,
        ending: false,
        ended: false,
        finished: false,
        destroyed: false,
        decodeStrings: false,
        defaultEncoding: 'utf8',
        length: 0,
        writing: false,
        corked: 0,
        sync: true,
        bufferProcessing: false,
        onwrite: [Function: bound onwrite],
        writecb: null,
        writelen: 0,
        bufferedRequest: null,
        lastBufferedRequest: null,
        pendingcb: 0,
        prefinished: false,
        errorEmitted: false,
        emitClose: false,
        autoDestroy: false,
        bufferedRequestCount: 0,
        corkedRequestsFree: [Object] },
     writable: false,
     allowHalfOpen: false,
     _sockname: null,
     _pendingData: null,
     _pendingEncoding: '',
     server: null,
     _server: null,
     [Symbol(asyncId)]: 218,
     [Symbol(lastWriteQueueSize)]: 0,
     [Symbol(timeout)]: null,
     [Symbol(kBytesRead)]: 0,
     [Symbol(kBytesWritten)]: 0 },
  stdin:
   Socket {
     connecting: false,
     _hadError: false,
     _handle:
      Pipe {
        domain: [Domain],
        onread: [Function: onStreamRead],
        [Symbol(owner)]: [Circular] },
     _parent: null,
     _host: null,
     _readableState:
      ReadableState {
        objectMode: false,
        highWaterMark: 16384,
        buffer: BufferList { head: null, tail: null, length: 0 },
        length: 0,
        pipes: null,
        pipesCount: 0,
        flowing: null,
        ended: false,
        endEmitted: false,
        reading: false,
        sync: true,
        needReadable: false,
        emittedReadable: false,
        readableListening: false,
        resumeScheduled: false,
        emitClose: false,
        autoDestroy: false,
        destroyed: false,
        defaultEncoding: 'utf8',
        awaitDrain: 0,
        readingMore: false,
        decoder: [StringDecoder],
        encoding: 'utf8' },
     readable: false,
     domain:
      Domain {
        domain: null,
        _events: [Object],
        _eventsCount: 3,
        _maxListeners: undefined,
        members: [] },
     _events:
      [Object: null prototype] { end: [Function: onReadableStreamEnd] },
     _eventsCount: 1,
     _maxListeners: undefined,
     _writableState:
      WritableState {
        objectMode: false,
        highWaterMark: 16384,
        finalCalled: true,
        needDrain: false,
        ending: true,
        ended: true,
        finished: false,
        destroyed: false,
        decodeStrings: false,
        defaultEncoding: 'utf8',
        length: 0,
        writing: false,
        corked: 0,
        sync: true,
        bufferProcessing: false,
        onwrite: [Function: bound onwrite],
        writecb: null,
        writelen: 0,
        bufferedRequest: null,
        lastBufferedRequest: null,
        pendingcb: 1,
        prefinished: false,
        errorEmitted: false,
        emitClose: false,
        autoDestroy: false,
        bufferedRequestCount: 0,
        corkedRequestsFree: [Object] },
     writable: false,
     allowHalfOpen: false,
     _sockname: null,
     _pendingData: null,
     _pendingEncoding: '',
     server: null,
     _server: null,
     [Symbol(asyncId)]: 217,
     [Symbol(lastWriteQueueSize)]: 0,
     [Symbol(timeout)]: null,
     [Symbol(kBytesRead)]: 0,
     [Symbol(kBytesWritten)]: 0 },
  stderr:
   Socket {
     connecting: false,
     _hadError: false,
     _handle:
      Pipe {
        domain: [Domain],
        onread: [Function: onStreamRead],
        reading: true,
        [Symbol(owner)]: [Circular] },
     _parent: null,
     _host: null,
     _readableState:
      ReadableState {
        objectMode: false,
        highWaterMark: 16384,
        buffer: BufferList { head: null, tail: null, length: 0 },
        length: 0,
        pipes: null,
        pipesCount: 0,
        flowing: true,
        ended: false,
        endEmitted: false,
        reading: true,
        sync: false,
        needReadable: true,
        emittedReadable: false,
        readableListening: false,
        resumeScheduled: true,
        emitClose: false,
        autoDestroy: false,
        destroyed: false,
        defaultEncoding: 'utf8',
        awaitDrain: 0,
        readingMore: false,
        decoder: [StringDecoder],
        encoding: 'utf8' },
     readable: true,
     domain:
      Domain {
        domain: null,
        _events: [Object],
        _eventsCount: 3,
        _maxListeners: undefined,
        members: [] },
     _events:
      [Object: null prototype] { end: [Array], close: [Function], data: [Function] },
     _eventsCount: 3,
     _maxListeners: undefined,
     _writableState:
      WritableState {
        objectMode: false,
        highWaterMark: 16384,
        finalCalled: false,
        needDrain: false,
        ending: false,
        ended: false,
        finished: false,
        destroyed: false,
        decodeStrings: false,
        defaultEncoding: 'utf8',
        length: 0,
        writing: false,
        corked: 0,
        sync: true,
        bufferProcessing: false,
        onwrite: [Function: bound onwrite],
        writecb: null,
        writelen: 0,
        bufferedRequest: null,
        lastBufferedRequest: null,
        pendingcb: 0,
        prefinished: false,
        errorEmitted: false,
        emitClose: false,
        autoDestroy: false,
        bufferedRequestCount: 0,
        corkedRequestsFree: [Object] },
     writable: false,
     allowHalfOpen: false,
     _sockname: null,
     _pendingData: null,
     _pendingEncoding: '',
     server: null,
     _server: null,
     [Symbol(asyncId)]: 219,
     [Symbol(lastWriteQueueSize)]: 0,
     [Symbol(timeout)]: null,
     [Symbol(kBytesRead)]: 0,
     [Symbol(kBytesWritten)]: 0 },
  _endCallback: [Function] }
> finished 

1 个答案:

答案 0 :(得分:0)

节点REPL(读取-评估-打印循环)将打印您键入的任何表达式的结果。例如:

> 1+1
2
> console.log("foo")
foo
undefined

1+1输出2,即使我们没有明确记录它也是如此。 console.log("foo")打印foo,但由于console.log返回undefined,因此REPL也会为您打印。{p>

runString的文档说它返回PythonShell实例。那就是你所看到的。将代码放入JavaScript文件并在REPL之外执行后,您将看不到它,就像当代码中包含undefined时看不到console.log("foo")

(您正在“完成”之前看到此 ,因为“完成”是异步打印的,而PythonShell则是同步返回其值。)