|
| 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 |
| 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.py", \ |
| 20 | + '--host=%s' % (options.host), \ |
| 21 | + '--properties=%s' % (options.properties), \ |
| 22 | + '--corenlp=%s' % (options.corenlp)] |
| 23 | + if not options.verbose: |
| 24 | + args += ['--quiet'] |
| 25 | + self.portCounter = 0 |
| 26 | + |
| 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(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', '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) |
| 51 | + filename = self.tempdir+key+".tmp" |
| 52 | + f = open(filename, 'w') |
| 53 | + f.write(text) |
| 54 | + f.close() |
| 55
23DA
| + self.sendThreadedRequest(key, currentPort) |
| 56 | + return {'status':'OK', 'key':key} |
| 57 | + |
| 58 | + def receive(self, blocking=False): |
| 59 | + """ Returns all completed parses. Set blocking to True on your last iteration! """ |
| 60 | + go = True |
| 61 | + response = [] |
| 62 | + while go: |
| 63 | + for key in self.processPool.keys(): |
| 64 | + process = self.processPool[key] |
| 65 | + if process.poll() != None: |
| 66 | + (out, error) = process.communicate() |
| 67 | + if out: |
| 68 | + try: |
| 69 | + response[key] = [json.loads(out)] |
| 70 | + except: |
| 71 | + pass |
| 72 | + del self.processPool[key] |
| 73 | + go = blocking and len(self.processPool) > 0 |
| 74 | + return response |
0 commit comments