Skip to content
This repository has been archived by the owner. It is now read-only.
Browse files
fix(service): make service handler thread-safe
  • Loading branch information
hanhxiao committed Oct 11, 2019
1 parent 51581bf commit c880c9b0bfe7173b61ae6489669fea202f7200d0
Showing with 23 additions and 10 deletions.
  1. +3 −0 gnes/cli/
  2. +20 −10 gnes/service/
@@ -288,6 +288,9 @@ def set_indexer_parser(parser=None):
if not parser:
parser = set_base_parser()
parser.add_argument('--as_response', type=ActionNoYes, default=True,
help='convert the message type from request to response after indexing. '
'turn it off if you want to chain other services after this index service.')

return parser

@@ -36,16 +36,20 @@ def _handler_index(self, msg: 'gnes_pb2.Message'):
# print('!!! tid: %s, tmp_a: %r %r' % (threading.get_ident(), self._tmp_a, self._handler_index))
from ..indexer.base import BaseChunkIndexer, BaseDocIndexer
if isinstance(self._model, BaseChunkIndexer):
is_changed = self._handler_chunk_index(msg)
elif isinstance(self._model, BaseDocIndexer):
is_changed = self._handler_doc_index(msg)
raise ServiceError(
'unsupported indexer, dont know how to use %s to handle this message' % self._model.__bases__)
msg.response.index.status = gnes_pb2.Response.SUCCESS

def _handler_chunk_index(self, msg: 'gnes_pb2.Message'):
if self.args.as_response:
msg.response.index.status = gnes_pb2.Response.SUCCESS

if is_changed:

def _handler_chunk_index(self, msg: 'gnes_pb2.Message') -> bool:
embed_info = []

for d in
@@ -59,13 +63,19 @@ def _handler_chunk_index(self, msg: 'gnes_pb2.Message'):
if embed_info:
vecs, doc_ids, offsets, weights = zip(*embed_info)
self._model.add(list(zip(doc_ids, offsets)), np.stack(vecs), weights)
return True
self.logger.warning('chunks contain no embedded vectors, the indexer will do nothing')

def _handler_doc_index(self, msg: 'gnes_pb2.Message'):
self._model.add([d.doc_id for d in],
[d for d in],
[d.weight for d in])
return False

def _handler_doc_index(self, msg: 'gnes_pb2.Message') -> bool:
self._model.add([d.doc_id for d in],
[d for d in],
[d.weight for d in])
return True
return False

def _put_result_into_message(self, results, msg: 'gnes_pb2.Message'):'topk_results')

0 comments on commit c880c9b

Please sign in to comment.