线性表的顺序存储算法实现

线性表顺序存储的类型定义

以下代码是顺序表的定义, 可以保存在 seqlist.h

数据类型定义

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
const int Maxsize = 100; // 定义一个足够大的常数
// 定义一个数据类型
typedef struct
{
    int num;
    char name[8];
} DataType;

// 定义一个存放数据的数组
typedef struct
{
    DataType data[Maxsize];
    int length;
} SeqList;

插入算法即将元素插入到顺序表 L 的第 i 个元素之前

VictoriaMetrics简单介绍与使用

VictoriaMetrics 是一种快速、经济高效且可扩展的监控解决方案和时间序列数据库

兼容 Prometheus及 PromQL

VictoriaMetrics 具有以下突出特点:

  • 可以作为 Prometheus 的长期存储

  • 可以作为 Grafana 中 Prometheus 的替代品,因为它支持 Prometheus 查询 API

Python paramiko的使用

使用paramiko在远程服务器上执行命令:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import paramiko
from typing import Dict


def ssh_command(hostname: str, port: str, username: str, password: str, command: str, timeout: int = 90,
                get_pty: bool = False) -> Dict:
    """
    连接远程服务器执行shell命令
    :param hostname: 远程服务器ip地址
    :param port: 指定远程服务器的ssh端口
    :param username: 远程用户名
    :param password: 密码
    :param command: 执行的命令
    :param timeout: 执行命令的超时时间
    :param get_pty: 是否启用tty 即伪终端
    :return:
    """
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    try:
        ssh.connect(hostname=hostname, port=port, username=username, password=password, timeout=8)
        stdin, stdout, stderr = ssh.exec_command(command=command, timeout=timeout, get_pty=get_pty)
        exit_code = stdout.channel.recv_exit_status()
        ssh_result = {
            'output': stdout.readlines(),
            'IP': hostname,
            'msg': 1,
            'error': stderr.readlines(),
            'exit_code': exit_code,

        }
        ssh.close()
    except Exception as e:
        ssh_result = {
            'output': 0,
            'IP': hostname,
            'msg': 0,
            'error': [str(e)],
            'exit_code': -1,
        }
        ssh.close()
    return ssh_result

其中get_pty 需要注意

Python与Rabbitmq

使用docker安装rabbitmq

拉取镜像

1
docker pull rabbitmq:management # 直接使用带web管理的

生成容器

1
2
mkdir /home/rabbitmq_data
docker run -itd --name rabbitmq -p 5672:5672 -p 15672:15672 -v /home/rabbitmq_data:/var/lib/rabbitmq -e RABBITMQ_DEFAULT_USER=admin -e RABBITMQ_DEFAULT_PASS=admin rabbitmq:management
  • RABBITMQ_DEFAULT_USER 用户名
  • RABBITMQ_DEFAULT_PASS 密码

访问web端

kombu模块

kombu官方站点

使用pip安装

1
pip install kombu

生产者

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
from kombu import Connection


def kombu_production(key, body, key_timeout=None):
    """
    rabbitmq 生产者
    :param key: 队列名
    :param body: 队列数据
    :param key_timeout: type:int 单位:s 超时时间 默认不过期
    :return:
    """
    MQ_URL = "amqp://admin:admin@192.168.1.100:5672//"
    connection = Connection(MQ_URL)
    channel = connection.Producer(serializer='json')
    channel.publish(body=body, routing_key=key, expiration=key_timeout)
    connection.release()
    

消费者

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from kombu import Connection, Queue


class Consumer(object):
    """任务队列消费者"""

    def __init__(self, queue_name):
        """
        :param queue_name: 队列名称
        """
        self.queue_name = queue_name
        self.connection = Connection('amqp://admin:admin@192.168.1.100:5672//')
        
    def run(self):
        """开始消费队列"""
        while True:
            try:
                with self.connection.Consumer(
                        queues=[Queue(self.queue_name, routing_key=self.queue_name)],
                        accept=['pickle', 'json'],
                        callbacks=[self.task_callbacks],
                        prefetch_count=1  # 每次取得一个消息
                ):
                    self.connection.drain_events(timeout=10)
            except socket.timeout as e:
                logger.error(f'运行时异常 {self.queue_name} {e}')
                break
            except Exception as e:
                logger.error(f'运行时异常 {self.queue_name} {e}')
                break

    @staticmethod
    def task_callbacks(body, message):
        """任务回调函数(重写)"""
        message.requeue()
        

class MyConsumer(Consumer):
    """
    自定义的一个消费者
    """
    
    def task_callbacks(body, message):
        if body:
            print(body)
        return message.ack
    
    
if __name__ == "__main__":
    my_consumer = MyConsumer('MyQueue')
    my_consumer.run()

以上即是python 与 rabbitmq 相关的代码片段