|
| 1 | +""" |
| 2 | +A load balancing platform for the CoreNLP python server. |
| 3 | +This allows us to keep multiple instances of the server open |
| 4 | +at different ports, and allow the same script to handle |
| 5 | +loadbalancing so client scripts need not worry about such logic. |
| 6 | +""" |
| 7 | + |
| 8 | +import os, requests, json, sys, jsonrpclib |
| 9 | +from subprocess import Popen, PIPE |
| 10 | +from hashlib import sha1 |
| 11 | + |
| 12 | +class CoreNLPLoadBalancer: |
| 13 | + def __init__(self, options): |
| 14 | + self.tempdir = "/tmp/" |
| 15 | + self.ports = options.ports.split(',') |
| 16 | + self.host = options.host |
| 17 | + self.serverPool = {} |
| 18 | + self.processPool = {} |
| 19 | + self.args = ["python", os.getcwd() + "/corenlp/corenlp.py", \ |
| 20 | + '--host=%s' % (options.host), \ |
| 21 | + '--properties=%s' % (options.properties), \ |
| 22 | + '--corenlp=%s' % (options.corenlp)] |
| 23 | + if not options.verbose: |
| 24 | + self.args += ['--quiet'] |
| 25 | + self.portCounter = 0 |
| 26 | + self.startup() |
| 27 | + |
| 28 | + def startup(self): |
| 29 | + """ Open a traditional server subprocess in a new port """ |
| 30 | + for port in self.ports: |
| 31 | + self.serverPool[port] = Popen(self.args + ['--port=%s' % str(port)]) |
| 32 | + |
| 33 | + def shutdown(self): |
| 34 | + for port in self.ports: |
| 35 | + self.serverPool[port].terminate() |
| 36 | + |
| 37 | + def sendThreadedRequest(self, key, port): |
| 38 | + """ Create a process that communicates with the server in a thread to avoid blocking """ |
| 39 | + host = 'http://%s:%s' % (self.host.replace('http://', ''), port) |
| 40 | + filename = self.tempdir+key+".tmp" |
| 41 | + self.processPool[key] = Popen(['python', os.getcwd()+'/corenlp/subserver.py', host, filename], stdout=PIPE) |
| 42 | + |
| 43 | + def send(self, text): |
| 44 | + """ |
| 45 | + Writes a temp file with the current text. The subserver script deletes this file for us. |
| 46 | + The response sent provides a sha1 key that corresponds to your requested document so we |
| 47 | + can correlate requests to responses. |
| 48 | + """ |
| 49 | + currentPort = self.ports[self.portCounter] |
| 50 | + key = sha1(text).hexdigest() |
| 51 | + filename = self.tempdir+key+".tmp" |
| 52 | + f = open(filename, 'w') |
| 53 | + f.write(text) |
| 54 | + f.close() |
| 55 | + self.sendThreadedRequest(key, currentPort) |
| 56 | + return {'status':'OK', 'key':key} |
| 57 | + |
| 58 | + def getCompleted(self): |
| 59 | + """ Returns all completed parses. Set blocking to True on your last iteration to get all data """ |
| 60 | + docResponse = {} |
| 61 | + response = {'status':'OK'} |
| 62 | + try: |
| 63 | + for key in self.processPool.keys(): |
| 64 | + print key |
| 65 | + process = self.processPool[key] |
| 66 | + print process |
| 67 | + if process.poll() != None: |
| 68 | + docResponse[key] = self.getForFinishedProcess(process) |
| 69 | + del self.processPool[key] |
| 70 | + except: |
| 71 | + response['status'] = 'ERROR' |
| 72 | + response['error'] = sys.exc_info()[1] |
| 73 | + response['parses'] = docResponse |
| 74 | + return response |
| 75 | + |
| 76 | + def getAll(self): |
| 77 | + """ Blocking counterpart to getCompleted. Wait for all currently open processes to complete and send response. """ |
| 78 | + response = {} |
| 79 | + for key in self.processPool.keys(): |
| 80 | + response[key] = self.getForKey(key) |
| 81 | + return {'status':'OK', 'parses':response} |
| 82 | + |
| 83 | + def getForFinishedProcess(self, process): |
| 84 | + """ Returns a dictionary with json string or empty dictionary """ |
| 85 | + response = {} |
| 86 | + print 'communicating' |
| 87 | + (out, error) = process.communicate() |
| 88 | + if out: |
| 89 | + try: |
| 90 | + response = json.loads(out) |
| 91 | + except: |
| 92 | + print sys.exc_info() |
| 93 | + pass |
| 94 | + return response |
| 95 | + |
| 96 | + def getForKey(self, key): |
| 97 | + """ Retrieves a response for a given key. This is blocking. """ |
| 98 | + response = {} |
| 99 | + if key in self.processPool.keys(): |
| 100 | + process = self.processPool[key] |
| 101 | + if process.poll == None: |
| 102 | + process.wait() |
| 103 | + response[key] = self.getForFinishedProcess(process) |
| 104 | + else: |
| 105 | + response[key] = {} |
| 106 | + return response |
0 commit comments