def main():

    # Benchmarking program
    t = Timer(verbose=True)

    # Dataset
    X = [[1], [2], [3], [4], [5], [6], [7]]
    Y = [1, 2, 3, 4, 5, 6, 7]

    # Minimizing using gradient descent method
    print("\n> Using Gradient Descent")
    lr = LinearRegression(X, Y, 0.03)
    t.start()
    lr.setAlpha(0.035706)
    lr.gradientDescent()
    t.finish()
    predicted_Y2 = [lr.predict(x) for x in X]
    print("Error rate: %f" % lr.getErrorRate())

    # Plotting example (2d graph)
    plt.plot(X, Y, 'o')  # Dots are original distribution
    plt.plot(X, predicted_Y2, 'g')  # Green line is Gradient descent
    plt.show()

    return 0
Exemplo n.º 2
0
def main():
    # 初始化
    timer = Timer()
    kafka_producer = producer.Producer()
    kafka_consumer = consumer.Consumer()

    # 到期callback
    def on_expired(message):
        print('Task expired!')
        target_topic = _.get(message, 'target_topic')
        if target_topic:
            kafka_producer.send(topic=target_topic, value=message)
            kafka_producer.flush()
        else:
            print('Enqueue: target_topic is missing in enqueue message!')

    # 订阅原始消息
    @kafka_consumer.event_emitter.on('message')
    def on_message(message):
        print('Receive delay task!')
        timer.add(
            TimerTaskEntry(delay=(_.get(message, 'value.delay') or 0),
                           task=on_expired,
                           message=message.value))

    # 恢复上一次停机前状态
    timer.recover(task=on_expired)
    # 持久化当前状态
    timer.persist()
    # 启动Timer
    timer.start()
    # 启动原始消息订阅
    kafka_consumer.tail()
Exemplo n.º 3
0
class Connection(multiprocessing.Process):
    def __init__(self,
                 sock,
                 node_listen_addr,
                 ip_addresses,
                 other_end_listen_addr=None):

        super(Connection, self).__init__()
        self.daemon = True
        self.socket = sock
        self.node_listen_addr = node_listen_addr
        self.other_end_listen_addr = other_end_listen_addr
        """
        ping_status:
        Integer telling how many ping messages have not been answered. Value -1 means that an answer has been
        received during that cycle.
        """
        self.ping_status = -1
        self.ping_timer = Timer(PING_INTERVAL)

        self.read_queue = multiprocessing.Queue()
        self.write_queue = multiprocessing.Queue()
        self.ip_addresses = ip_addresses

    def run(self):
        process_print("New Connection() process running")

        # If socket is not created already, do it now.
        if self.socket is None:
            self.socket = connect(self.other_end_listen_addr[0],
                                  self.other_end_listen_addr[1])
            if self.socket is None:
                self.die()

            self.send_join_request()
            self.recv_join_response()
            self.register_connection(self.other_end_listen_addr)
        else:
            join_request = self.recv_join_request()
            self.other_end_listen_addr = (join_request.header.sender_ip,
                                          join_request.header.sender_port)
            self.send_join_response(join_request)
            self.register_connection(self.other_end_listen_addr)

        self.ping_timer.start()

        while True:
            if self.ping_timer.has_expired():
                self.ping_status += 1
                self.send_ping_a()
                self.ping_timer.start()

            read_sockets, _, _ = select.select(
                [self.read_queue._reader, self.socket], [], [], PING_INTERVAL)
            for sock in read_sockets:
                if sock == self.socket:
                    try:
                        header = self.recv_header()
                    except ValueError:
                        continue
                    body = recvall(self.socket, header.payload_len)

                    if header.msg_type == TYPE_QUERY_HIT:
                        self.write_queue.put(Message(header, body, None))

                    elif header.msg_type == TYPE_PING:
                        if header.time_to_live == 1:
                            self.respond_to_type_a_ping(header.msg_id)
                        else:
                            pass  # TODO: receive type b pings

                    elif header.msg_type == TYPE_PONG:
                        # Type A pong
                        if len(body) == 0:
                            self.ping_status = -1
                        else:
                            pass  # TODO: receive type b pongs

                    elif header.msg_type == TYPE_QUERY:
                        self.write_queue.put(
                            Message(header, body, [],
                                    [self.other_end_listen_addr]))

                else:
                    self.handle_read_queue()

    def handle_read_queue(self):
        message = self.read_queue.get()
        if isinstance(message, Message):
            self.send_message(message)

    def respond_to_type_a_ping(self, msg_id):
        header_obj = Header(TYPE_PONG, self.node_listen_addr[0],
                            self.node_listen_addr[1], 0, 1, msg_id)
        byte_header = header_obj.get_bytes()
        try:
            self.socket.sendall(byte_header)
        except socket.error:
            self.die()

    def recv_join_request(self):
        """
        :return: listen address (ip, port tuple) of the sender
        """
        header = self.recv_header()
        return Message(header)
        # TODO: validation

    def send_message(self, message):
        byte_header = message.header.get_bytes()
        try:
            self.socket.sendall(byte_header)
            self.socket.sendall(message.body)
            # process_print("sent message: " + str(byte_header) + " and body : " + str(message.body))  # DEBUG
        except socket.error:
            self.die()

    def send_join_request(self):
        header_obj = Header(TYPE_JOIN, self.node_listen_addr[0],
                            self.node_listen_addr[1], 0, 1)
        byte_header = header_obj.get_bytes()
        try:
            self.socket.sendall(byte_header)
        except socket.error:
            self.die()

    def send_ping_a(self):
        header_obj = Header(TYPE_PING, self.node_listen_addr[0],
                            self.node_listen_addr[1], 0, 1)
        byte_header = header_obj.get_bytes()
        try:
            self.socket.sendall(byte_header)
        except socket.error:
            self.die()

    def recv_join_response(self):
        header = self.recv_header()
        bytes_payload = recvall(self.socket, JOIN_RESPONSE_PAYLOAD_LEN)
        payload = struct.unpack("!H", bytes_payload)[0]
        if payload != JOIN_ACC:
            self.die()

    def send_join_response(self, request):
        header_obj = Header(TYPE_JOIN, self.node_listen_addr[0],
                            self.node_listen_addr[1],
                            JOIN_RESPONSE_PAYLOAD_LEN, 1,
                            request.header.msg_id)
        payload_bytes = struct.pack("!H", JOIN_ACC)
        try:
            self.socket.sendall(header_obj.get_bytes() + payload_bytes)
        except socket.error:
            self.die()

    def recv_header(self):
        bytes_header = recvall(self.socket, HEADER_LENGTH)
        if bytes_header is None:
            self.die()
        header_obj = unpack_header(bytes_header)
        return header_obj

    def register_connection(self, other_end_listen_addr):
        registration = InterprocessMessage(InterprocessMessage.REGISTER,
                                           other_end_listen_addr)
        self.write_queue.put(registration)

    def die(self):
        if self.socket is not None:
            self.socket.close()
        process_print("Process died")
        os._exit(0)