2017-08-08 98 views
2

我在從節點js在EC2 Ubuntu上運行python子進程時出現錯誤。生成的子進程永遠不會被node.js調用,然而事情在本地工作上完美無缺。 我們最初都拿到一個EPIPE錯誤,直到我們使用「命令和apt-get安裝libfontconfig」,然後在下面產生這個錯誤:錯誤:從nodejs運行python子進程時讀取ECONNRESET

events.js:160 
     throw er; // Unhandled 'error' event 
    ^//error Error: read ECONNRESET 
    at exports._errnoException (util.js:1020:11) 
    at Pipe.onread (net.js:568:26) 



     //child process    
     urlCrawlJob(hostname, pageCounter+1, accessToken); //recursive calling of the function 


     var process = SPAWN('python', [PATH.join(__dirname,"../pyScripts/crawler.py")]), 

     data = body.customers; 
     dataString = ''; 

     console.log(`Spawned child pid: ${process.pid}`); 
     process.stdout.on('error', function (err) { 
      console.log('stdout error: ', err); 
      console.log(err.code); 
     }); 

     process.stdout.on('data', function(data){ 
      dataString+=data 
      console.log(dataString); 
     }); 
     process.stdout.on('end', function(){ 

      console.log("ending child process -----> call url"); 

     }); 
     process.stdin.write(JSON.stringify({"data":data,"hostname":hostname})); 
     process.stdin.end();     

    //python script 

## process_init.py 

#crete seperate function, impletment oop concepet 

import sys, json, pymongo, os 


#defaukt address, email, phnNo, total spe 

print "==> in crawler python" 

#print sys.args[0]; 

def main(): 
    data = json.load(sys.stdin) 
    hostname = data['hostname']; 
    customerData = data['data']; 

    print hostname 


    collection = dbConnection(hostname) 


    #isFile = open("/crawlerResult/"+hostname+".txt","w+") 


    pwd = os.getcwd() 

    print pwd 
    file = open(pwd+"/crawlerResult/"+hostname+".txt", "a++") 

    iteratingData(customerData, file, collection) 


    print "=====\n\n" 
    client.close();        #close the db connection 
    sys.stdout.flush(); 

def dbConnection(hostname): 

    #opening connection with db 
    client = pymongo.MongoClient('127.0.0.1', 27017) ;       # TODO: if connection already open do not open neew one 
    # print client 
    db = client["customerLTV"]; 
    print db 
    collection = db[hostname]; 
    return collection 


def iteratingData(customerData, file, collection): 

    count = 0 
    data = {} 
    for i in customerData: 
     #print i 
     count += 1 
     #print len(i["addresses"]); 

     try: 


      strdata = {} 
      strdata[count] = data 
      strdata = json.dumps(strdata) 
      file.write(strdata + "\n")     


      data.pop('_id', None) 
      collection.insert(data)   
      data = {} 
     except Exception, e: 
      print str(e) 

    print count; 




#start process 
if __name__ == '__main__': 
    main() 

回答

0

它看起來像你的模塊並不一定是Ubuntu的服務器上安裝正確。您是否安裝或更新了所有的Ubuntu EC2模塊?特別是如果Python子進程沒有運行。

+0

嘿,謝謝我正在考慮你的解決方案,這對我來說似乎是正確的,因爲這些模塊並不存在......你拯救了我的一天。 – Kushan