def __enter__(self): self.usr.run(['user', 'create', self.uname]) self.usr.run(['silo', 'create', 'filesystem', 's']) self.usr.run(['network', 'create', 'n', '-S', 's', '--as', self.uname]) self.usr.run( ['kvs', 'create', self.kvname, '-N', 'n', '--as', self.uname]) port_file = '%s/port' % self.usr.dir self.__proc = self.usr.spawn([ 'memo', 'kvs', 'run', self.kvname, '--as', self.uname, '--allow-root-creation', '--grpc', '127.0.0.1:0', '--grpc-port-file', port_file ]) def comm(self): self.out, self.err = self.__proc.communicate() import threading self.__comm = threading.Thread(target=comm, args=[self]) self.__comm.start() while not os.path.exists(port_file): time.sleep(0.1) with open(port_file, 'r') as f: self.__endpoint = '127.0.0.1:{}'.format(f.readline().strip()) import grpc import memo_kvs_pb2_grpc channel = grpc.insecure_channel(self.__endpoint) self.__stub = memo_kvs_pb2_grpc.KeyValueStoreStub(channel) return self
def client(self): import grpc import memo_kvs_pb2_grpc channel = grpc.insecure_channel(self.__endpoint) return memo_kvs_pb2_grpc.KeyValueStoreStub(channel)
#!/usr/bin/env python3 import grpc import memo_kvs_pb2_grpc import memo_kvs_pb2 as kvs import sys if __name__ == "__main__": channel = grpc.insecure_channel(sys.argv[1]) store = memo_kvs_pb2_grpc.KeyValueStoreStub(channel) def insert(key, value): store.Insert(kvs.InsertRequest(key=key, value=value.encode('utf-8'))) def update(key, value): store.Update(kvs.UpdateRequest(key=key, value=value.encode('utf-8'))) def upsert(key, value): store.Upsert(kvs.UpsertRequest(key=key, value=value.encode('utf-8'))) def fetch(key): return store.Fetch(kvs.FetchRequest(key=key)).value.decode('utf-8') def delete(key): store.Delete(kvs.DeleteRequest(key=key)) def list_(): return list(map(lambda i: i.key, store.List(kvs.ListRequest()).items)) command = sys.argv[2] if command == 'insert':