title: Continuously exchange data using python-shell
body: <p>I need to run some python scripts from node. Since my python scripts use complex structures I figured would be better if I only loaded those structures once and then run some the specific scripts (tasks) using the structures.</p>
  
  <p>On node I want to run a script forever (or until I say it can terminate) and keep sending on-demand messages to this script. This script would create a process, using python <code>multiprocessing</code>, to run the specific task and start listening again.</p>
  
  <p>A use case would be:</p>
  
  <ul>
  <li>Node starts, and wakes python script</li>
  <li>Some action on the node client causes it to send a <code>command</code> to the python script</li>
  <li>The python script computes what it needs and returns the data</li>
  </ul>
  
  <p>I think <code>python-shell</code> can help me with this</p>
  
  <p>On node I have something like this:</p>
  
  <pre><code>var app = express();
  app.listen(8000, function () {
      console.log('Example app listening on port 8000!');
  });
  
  var pyshell = new PythonShell('start.py', options);
  pyshell.on('message', function (message) {
      handleAnswer(message);
  });
  
  pyshell.end(function (err, code, signal) {
      handleEnd(err, code, signal);
  });
  
  app.get('/hello', function (req, res) {
      pyshell.send('hello');
  });
  </code></pre>
  
  <p>My <code>start.py</code> script is:</p>
  
  <pre><code>import sys
  
  def listen():
      while True:
          command = sys.stdin.readline()
          command = command.split('\n')[0]
          if command:
              print("Received CMD " + command)
  
  if __name__ == '__main__':
      data = json.loads(sys.argv[1])
      listen()
  </code></pre>
  
  <p>I tried the solution on this <a href="https://stackoverflow.com/questions/42462072/pythonshell-in-node-nwjs">similar question</a> but gets and error when wrapping the <code>hello</code> message in an endpoint.</p>
  
  <p>I properly require <code>python-shell</code> module and I'm able to execute a python script and return data (using <code>print</code>). My problem is to start the script and send the message after a (random) time period.</p>
  
  <p>When I open <code>localhost:8000/hello</code> I get <code>Error: write after end</code></p>
  
  <p>Full error:</p>
  
  <pre><code>    Error: write after end
      at writeAfterEnd (_stream_writable.js:236:12)
      at Socket.Writable.write (_stream_writable.js:287:5)
      at Socket.write (net.js:717:40)
      at PythonShell.send (C:\Users\leonardo.schettini\Documents\recrutai\client\node_modules\python-shell\index.js:206:16)
      at C:\Users\leonardo.schettini\Documents\recrutai\client\app.js:27:12
      at Layer.handle [as handle_request] (C:\Users\leonardo.schettini\Documents\recrutai\client\node_modules\express\lib\router\layer.js:95:5)
      at next (C:\Users\leonardo.schettini\Documents\recrutai\client\node_modules\express\lib\router\route.js:137:13)
      at Route.dispatch (C:\Users\leonardo.schettini\Documents\recrutai\client\node_modules\express\lib\router\route.js:112:3)
      at Layer.handle [as handle_request] (C:\Users\leonardo.schettini\Documents\recrutai\client\node_modules\express\lib\router\layer.js:95:5)
      at C:\Users\leonardo.schettini\Documents\recrutai\client\node_modules\express\lib\router\index.js:281:22
  </code></pre>