extrabacon / python-shell

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

Unable to handle streaming of data and returns error every time even though response is sent !!! #72

Closed shubsaini09 closed 7 years ago

shubsaini09 commented 7 years ago

I am trying to use adwords api python scripts from hapi.js using python-shell. Now when i write a simple main() function with or without arguments i get the output properly on my screen but when i use the api code i get an error but i still get the error.

controller.js

var PythonShell = require('python-shell');
PythonShell.defaultOptions = { scriptPath: './server/python/' };

exports.adwords =  {
  handler: function(request, reply){
    PythonShell.run('script.py', function (err, res) {
      if (err) throw err;
      reply(res); 
    });
});

script.py

import logging
import os
import sys, json
import csv
from googleads import adwords

logging.basicConfig(level=logging.INFO)
logging.getLogger('suds.transport').setLevel(logging.DEBUG)

def main(client):
  #-----sample code-----
  file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)),"report.csv")
  f = open(file_path,'wb')
  f.write('Bacon is not a vegetable')
  f.close()

if __name__ == '__main__':
  adwords_client = adwords.AdWordsClient.LoadFromStorage()
  adwords_client.SetClientCustomerId('111-111-1111') 
  main(adwords_client)

Error which i get:

{ [Error: INFO:oauth2client.client:Refreshing access_token
]
  executable: 'python',
  options: null,
  script: 'server/python/script.py',
  args: null,
  exitCode: 0 }

My guess is python-shell is not able to handle streaming of data so it considers the first line of the output as error and returns. Also api processing happens with some async code.

pewh commented 6 years ago

I face same issue too. Would you mind to share your workaround? Thank you