-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from dvzubarev/multiprocessing
Introduce multiprocessing
- Loading branch information
Showing
15 changed files
with
203 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,84 @@ | ||
import multiprocessing | ||
|
||
from . import annotation_pb2 as pb | ||
from . import annotation_pb2_grpc | ||
from . import annotation_to_protobuf | ||
from . import annotation_from_protobuf | ||
from .pipeline_common import PipelineCommon | ||
|
||
import grpc | ||
import logging | ||
|
||
|
||
logger = logging.getLogger('isanlp') | ||
|
||
def _expand_ppl(ppl): | ||
res_dict = {k : v[0] for k, v in ppl.get_processors().items()} | ||
return res_dict | ||
|
||
PPLS = None | ||
def _init_process(ppls): | ||
global PPLS | ||
PPLS = ppls | ||
standalone_procs = {} | ||
for ppl in ppls.values(): | ||
if not isinstance(ppl, PipelineCommon): | ||
continue | ||
for proc in ppl.processors_iter(): | ||
if hasattr(proc, 'init'): | ||
proc.init() | ||
standalone_procs.update(_expand_ppl(ppl)) | ||
PPLS.update(standalone_procs) | ||
|
||
def _process_input(ppl_name, input_annotations): | ||
ppl = PPLS[ppl_name] | ||
return ppl(*input_annotations) | ||
|
||
|
||
|
||
class NlpService(annotation_pb2_grpc.NlpServiceServicer): | ||
"""Basic NLP gRPC annotation service. | ||
Args: | ||
ppls: dictionary {<pipeline name> : <pipeline object>} | ||
""" | ||
|
||
def __init__(self, ppls): | ||
self._ppls = ppls | ||
|
||
|
||
def __init__(self, ppls, max_workers = 1): | ||
self._pool = multiprocessing.Pool(processes=max_workers, | ||
initializer = _init_process, | ||
initargs = (ppls,) ) | ||
|
||
|
||
|
||
def process(self, request, context): | ||
"""(gRPC method) Processes text document with a specified pipeline. | ||
The request contains the name of a pipeline to invoke and required annotations. | ||
""" | ||
|
||
logger.info('Processing incoming request with "{}"...'.format(request.pipeline_name)) | ||
ppl = self._ppls[request.pipeline_name] | ||
input_annotations = annotation_from_protobuf.convert_annotation(request.input_annotations) | ||
res = ppl(*input_annotations) | ||
logger.info('Done.') | ||
|
||
|
||
logger.info('Processing incoming request with "{}"...'.format(request.pipeline_name)) | ||
res = self._pool.apply(_process_input, args=(request.pipeline_name, input_annotations)) | ||
pb_res = annotation_to_protobuf.convert_annotation(res) | ||
reply = pb.ProcessReply() | ||
reply.output_annotations.Pack(pb_res) | ||
|
||
return reply | ||
|
||
|
||
|
||
|
||
def get_registered_pipelines(self, request, context): | ||
"""(gRPC method) Outputs pipelines registered in the service.""" | ||
|
||
repl = RegisteredPipelinesReply() | ||
for e in self._ppl.keys(): | ||
repl.add(e) | ||
return repl | ||
|
||
def add_to_server(self, server): | ||
"""Is required for adding service to server. | ||
Is invoked by NlpServiceServer. | ||
""" | ||
|
||
annotation_pb2_grpc.add_NlpServiceServicer_to_server(self, server) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.