client.py 1.3 KB
Newer Older
1 2 3
import ctypes
import os

4
path = os.path.join(os.path.dirname(__file__), "libpaddle_master.so")
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
lib = ctypes.cdll.LoadLibrary(path)


class client(object):
    """
    client is a client to the master server.
    """

    def __init__(self, addr, buf_size):
        self.c = lib.paddle_new_master_client(addr, buf_size)

    def close(self):
        lib.paddle_release_master_client(self.c)
        self.c = None

    def set_dataset(self, paths):
        holder_type = ctypes.c_char_p * len(paths)
        holder = holder_type()
        print paths
        for idx, path in enumerate(paths):
            c_ptr = ctypes.c_char_p(path)
            holder[idx] = c_ptr
        lib.paddle_set_dataset(self.c, holder, len(paths))

G
gongweibao 已提交
29 30 31 32
    # return format: (record, errno)
    # errno =  0: ok
    #       = -1: EOF
    #       < -1: error
33 34 35 36
    def next_record(self):
        p = ctypes.c_char_p()
        ret = ctypes.pointer(p)
        size = lib.paddle_next_record(self.c, ret)
G
gongweibao 已提交
37
        if size == -1:
G
gongweibao 已提交
38
            # EOF
G
gongweibao 已提交
39 40 41 42 43 44
            return None, -1

        if size < -1:
            # Error
            return None, size

45
        if size == 0:
H
Helin Wang 已提交
46
            # Empty record
G
gongweibao 已提交
47 48
            return "", 0

49
        record = ret.contents.value[:size]
H
Helin Wang 已提交
50
        # Memory created from C should be freed.
51
        lib.mem_free(ret.contents)
G
gongweibao 已提交
52
        return record, 0