- Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathPythonConnector.js
More file actions
Latest commit
37 lines (30 loc) · 1.35 KB
/
Copy pathPythonConnector.js
File metadata and controls
37 lines (30 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
constspawn=require('child_process').spawn;
constpath=require('path');
constzerorpc=require('zerorpc');
constUtils=require('./Utils.js');
constTIMEOUT=60;// in seconds (adjust as per how long your server calls can take)
constIP='127.0.0.1';
constPORT='4242';
classPythonConnector{
staticserver(){
if(!PythonConnector.connected){
console.log('PythonConnector – making a new connection to the python layer');
PythonConnector.zerorpcProcess=spawn('python3',['-u',path.join(__dirname,'PythonServer.py')]);
PythonConnector.zerorpcProcess.stdout.on('data',function(data){
console.info('python:',data.toString());
});
PythonConnector.zerorpcProcess.stderr.on('data',function(data){
console.error('python:',data.toString());
});
PythonConnector.zerorpc=newzerorpc.Client({'timeout': TIMEOUT,'heartbeatInterval': TIMEOUT*1000});
PythonConnector.zerorpc.connect('tcp://'+IP+':'+PORT);
PythonConnector.connected=true;
}
returnPythonConnector.zerorpc;
}
staticasyncinvoke(method, ...args){
varzerorpc=PythonConnector.server();
returnawaitUtils.promisify(zerorpc.invoke,zerorpc,method, ...args);
}
}
module.exports=PythonConnector;