コード例 #1
0

if __name__ == '__main__':
    bc = BertClient(port=int(sys.argv[1]), port_out=int(sys.argv[2]))
    num_repeat = 20

    with open('../data/README.md') as fp:
        data = [v for v in fp if v.strip()]

    send_without_block(bc, data, num_repeat)

    num_expect_vecs = len(data) * num_repeat

    # then fetch all
    print('now waiting until all results are available...')
    vecs = bc.fetch_all(concat=True)
    print('received %s, expected: %d' % (vecs.shape, num_expect_vecs))

    # now send it again
    send_without_block(bc, data, num_repeat)

    # this time fetch them one by one, due to the async encoding and server scheduling
    # sending order is NOT preserved!
    for v in bc.fetch():
        print('received %s, shape %s' % (v.id, v.content.shape))

    # finally let's do encode-fetch at the same time but in async mode
    # we do that by building an endless data stream, generating data in an extremely fast speed
    def text_gen():
        while True:
            yield data