extrabacon / python-shell

Run Python scripts from Node.js with simple (but efficient) inter-process communication through stdio
2.11k stars 224 forks source link

Python Errors stops node... #175

Open MacTheZazou opened 5 years ago

MacTheZazou commented 5 years ago

I try to run a script and communicate with Python, but if Python gets an error, it always crashes node... How can I handle those exceptions?

function init(pwmStore) {
  store = pwmStore;
  pwmMap = store.getState().pwmMap;
  store.subscribe(handleChange);
  try {
    pyshell = new PythonShell('./src/python/pwmServer.py', {mode: 'json', pythonPath: 'python3'});

  } catch (e) {
    console.log(e);
  }
  pyshell.on('stderr', function (stderr) {
    console.log(stderr);
  });
  pyshell.on('error', function (err) {
    console.log(err);
  });
  pyshell.on('message', function (message) {
    console.log(message);
  });
  pyshell.on('close', function (message) {
    console.log(message);
  });
}

The error:

Traceback (most recent call last):
  File "/home/pi/.local/lib/python3.5/site-packages/adafruit_bus_device/i2c_device.py", line 64, in __init__
    i2c.writeto(device_address, b'')
  File "/home/pi/.local/lib/python3.5/site-packages/busio.py", line 65, in writeto
    return self._i2c.writeto(address, buffer, stop=stop)
  File "/home/pi/.local/lib/python3.5/site-packages/adafruit_blinka/microcontroller/generic_linux/i2c.py", line 38, in writeto
    self._i2c_bus.write_bytes(address, buffer[start:end])
  File "/home/pi/.local/lib/python3.5/site-packages/Adafruit_PureIO/smbus.py", line 244, in write_bytes
    self._device.write(buf)
OSError: [Errno 121] Remote I/O error

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "src/python/pwmServer.py", line 12, in <module>
    pwm = PCA9685(i2c_bus)
  File "/home/pi/.local/lib/python3.5/site-packages/adafruit_pca9685.py", line 131, in __init__
    self.i2c_device = i2c_device.I2CDevice(i2c_bus, address)
  File "/home/pi/.local/lib/python3.5/site-packages/adafruit_bus_device/i2c_device.py", line 66, in __init__
    raise ValueError("No I2C device at address: %x" % device_address)
ValueError: No I2C device at address: 40
{ Error: ValueError: No I2C device at address: 40
    at PythonShell.parseError (/home/pi/koada/node_modules/python-shell/index.js:246:21)
    at terminateIfNeeded (/home/pi/koada/node_modules/python-shell/index.js:129:32)
    at ChildProcess.<anonymous> (/home/pi/koada/node_modules/python-shell/index.js:121:13)
    at ChildProcess.emit (events.js:188:13)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:254:12)
    ----- Python Traceback -----
    File "/home/pi/.local/lib/python3.5/site-packages/adafruit_bus_device/i2c_device.py", line 64, in __init__
      i2c.writeto(device_address, b'')
    File "/home/pi/.local/lib/python3.5/site-packages/busio.py", line 65, in writeto
      return self._i2c.writeto(address, buffer, stop=stop)
    File "/home/pi/.local/lib/python3.5/site-packages/adafruit_blinka/microcontroller/generic_linux/i2c.py", line 38, in writeto
      self._i2c_bus.write_bytes(address, buffer[start:end])
    File "/home/pi/.local/lib/python3.5/site-packages/Adafruit_PureIO/smbus.py", line 244, in write_bytes
      self._device.write(buf)
  OSError: [Errno 121] Remote I/O error

  During handling of the above exception, another exception occurred:

  Traceback (most recent call last):
    File "src/python/pwmServer.py", line 12, in <module>
      pwm = PCA9685(i2c_bus)
    File "/home/pi/.local/lib/python3.5/site-packages/adafruit_pca9685.py", line 131, in __init__
      self.i2c_device = i2c_device.I2CDevice(i2c_bus, address)
    File "/home/pi/.local/lib/python3.5/site-packages/adafruit_bus_device/i2c_device.py", line 66, in __init__
      raise ValueError("No I2C device at address: %x" % device_address)
  traceback:
   `Traceback (most recent call last):\n  File "/home/pi/.local/lib/python3.5/site-packages/adafruit_bus_device/i2c_device.py", line 64, in __init__\n    i2c.writeto(device_address, b'')\n  File "/home/pi/.local/lib/python3.5/site-packages/busio.py", line 65, in writeto\nreturn self._i2c.writeto(address, buffer, stop=stop)\n  File "/home/pi/.local/lib/python3.5/site-packages/adafruit_blinka/microcontroller/generic_linux/i2c.py", line 38, in writeto\n    self._i2c_bus.write_bytes(address, buffer[start:end])\n  File "/home/pi/.local/lib/python3.5/site-packages/Adafruit_PureIO/smbus.py", line 244, in write_bytes\n    self._device.write(buf)\nOSError: [Errno 121] Remote I/O error\n\nDuring handling of the above exception, another exception occurred:\n\nTraceback (most recent call last):\n  File "src/python/pwmServer.py", line 12, in <module>\n    pwm = PCA9685(i2c_bus)\n  File "/home/pi/.local/lib/python3.5/site-packages/adafruit_pca9685.py", line 131, in __init__\n    self.i2c_device = i2c_device.I2CDevice(i2c_bus, address)\n  File "/home/pi/.local/lib/python3.5/site-packages/adafruit_bus_device/i2c_device.py", line 66, in __init__\n    raise ValueError("No I2C device at address: %x" % device_address)\nValueError: No I2Cdevice at address: 40\n`,
  executable: 'python3',
  options: null,
  script: 'src/python/pwmServer.py',
  args: null,
  exitCode: 1 }
undefined

Thank you very much

Almenon commented 5 years ago

odd... are you sure that's an actual error and not just the logging of the error when you do console.log?

Try adding a console.log("Finished") at the end and see if that gets called.

sivachaitanya commented 5 years ago

i'm facing the similar issue too, if there is syntax error its crashing the node

felipap commented 5 years ago

Same here. Are there alternatives to this library?

groubis commented 4 years ago

Same here...

muyoungko commented 4 years ago

In my case, define error prevent node process exit. let pyshell = new PythonShell(filename, options);

pyshell.on('error', function (err) { console.log(' error ', err); })

Additionally, I also defined

pyshell.on('message' pyshell.on('stderr' pyshell.on('error' pyshell.on('close'

LeonardoRick commented 4 years ago

This worked for me too @muyoungko. Saved my life! Thanks