def send(command, data):
    """Send command to Training Service.
    command: CommandType object.
    data: string payload.
    """
    global _lock
    try:
        _lock.acquire()
        data = data.encode('utf8')
        assert len(data) < 1000000, 'Command too long'
        msg = b'%b%06d%b' % (command.value, len(data), data)
        logging.getLogger(__name__).debug('Sending command, data: [%s]' % msg)
        _out_file.write(msg)
        _out_file.flush()
    finally:
        _lock.release()