下载aioredis 为了提升性能我们使用一部redis
pip install aioredis
demo:
import asyncio
import aioredis
# 每隔10s获取redis信息
async def monitor_redis(host, port, interval):
# 建立异步Redis连接
redis_uri = f"redis://{host}:{port}"
redis = await aioredis.from_url(redis_uri, decode_responses=True)
while True:
try:
# 获取连接数和内存使用信息
info = await redis.info()
print(info) # 打印监控信息
except aioredis.RedisError as e:
await redis.close()
print("Error connecting to Redis:", e)
# 等待指定的时间间隔
await asyncio.sleep(interval)
async def main():
redis_servers = [
{"host": "localhost", "port": 6379},
# 添加更多的Redis服务器信息
]
monitoring_interval = 10 # 监控间隔(秒)
tasks = []
for server in redis_servers:
task = asyncio.create_task(monitor_redis(server["host"], server["port"], monitoring_interval))
tasks.append(task)
await asyncio.gather(*tasks)
if __name__ == "__main__":
asyncio.run(main())
{'redis_version': '5.0.9', 'redis_git_sha1': '9414ab9b', 'redis_git_dirty': 0, 'redis_build_id': '25845e7feb545d77', 'redis_mode': 'standalone', 'os': 'Windows ', 'arch_bits': 64, 'multiplexing_api': 'WinSock_IOCP', 'atomicvar_api': 'pthread-mutex', 'process_id': 4248, 'run_id': '258f17c72776228a0c943ae0a1aa928191c76f39', 'tcp_port': 6379, 'uptime_in_seconds': 112002, 'uptime_in_days': 1, 'hz': 10, 'configured_hz': 10, 'lru_clock': 14622326, 'executable': 'D:\\Redis\\"d:\\redis\\redis-server.exe"', 'config_file': 'D:\\Redis\\redis.windows-service.conf', 'connected_clients': 1, 'client_recent_max_input_buffer': 2, 'client_recent_max_output_buffer': 0, 'blocked_clients': 0, 'used_memory': 757704, 'used_memory_human': '739.95K', 'used_memory_rss': 694808, 'used_memory_rss_human': '678.52K', 'used_memory_peak': 757704, 'used_memory_peak_human': '739.95K', 'used_memory_peak_perc': '100.00%', 'used_memory_overhead': 715590, 'used_memory_startup': 665136, 'used_memory_dataset': 42114, 'used_memory_dataset_perc': '45.50%', 'allocator_allocated': 1150072, 'allocator_active': 322961408, 'allocator_resident': 486539264, 'total_system_memory': 0, 'total_system_memory_human': '0B', 'used_memory_lua': 37888, 'used_memory_lua_human': '37.00K', 'used_memory_scripts': 0, 'used_memory_scripts_human': '0B', 'number_of_cached_scripts': 0, 'maxmemory': 209715200, 'maxmemory_human': '200.00M', 'maxmemory_policy': 'noeviction', 'allocator_frag_ratio': 280.82, 'allocator_frag_bytes': 321811336, 'allocator_rss_ratio': 1.51, 'allocator_rss_bytes': 163577856, 'rss_overhead_ratio': 0.0, 'rss_overhead_bytes': -485844456, 'mem_fragmentation_ratio': 1.0, 'mem_fragmentation_bytes': 0, 'mem_not_counted_for_evict': 0, 'mem_replication_backlog': 0, 'mem_clients_slaves': 0, 'mem_clients_normal': 49950, 'mem_aof_buffer': 0, 'mem_allocator': 'jemalloc-5.2.1', 'active_defrag_running': 0, 'lazyfree_pending_objects': 0, 'loading': 0, 'rdb_changes_since_last_save': 0, 'rdb_bgsave_in_progress': 0, 'rdb_last_save_time': 1692231924, 'rdb_last_bgsave_status': 'ok', 'rdb_last_bgsave_time_sec': -1, 'rdb_current_bgsave_time_sec': -1, 'rdb_last_cow_size': 0, 'aof_enabled': 0, 'aof_rewrite_in_progress': 0, 'aof_rewrite_scheduled': 0, 'aof_last_rewrite_time_sec': -1, 'aof_current_rewrite_time_sec': -1, 'aof_last_bgrewrite_status': 'ok', 'aof_last_write_status': 'ok', 'aof_last_cow_size': 0, 'total_connections_received': 7, 'total_commands_processed': 9, 'instantaneous_ops_per_sec': 0, 'total_net_input_bytes': 200, 'total_net_output_bytes': 25132, 'instantaneous_input_kbps': 0.0, 'instantaneous_output_kbps': 0.0, 'rejected_connections': 0, 'sync_full': 0, 'sync_partial_ok': 0, 'sync_partial_err': 0, 'expired_keys': 0, 'expired_stale_perc': 0.0, 'expired_time_cap_reached_count': 0, 'evicted_keys': 0, 'keyspace_hits': 0, 'keyspace_misses': 0, 'pubsub_channels': 0, 'pubsub_patterns': 0, 'latest_fork_usec': 0, 'migrate_cached_sockets': 0, 'slave_expires_tracked_keys': 0, 'active_defrag_hits': 0, 'active_defrag_misses': 0, 'active_defrag_key_hits': 0, 'active_defrag_key_misses': 0, 'role': 'master', 'connected_slaves': 0, 'master_replid': 'a28318958e19a47d60009b7f5604c77d4297909a', 'master_replid2': 0, 'master_repl_offset': 0, 'second_repl_offset': -1, 'repl_backlog_active': 0, 'repl_backlog_size': 1048576, 'repl_backlog_first_byte_offset': 0, 'repl_backlog_histlen': 0, 'used_cpu_sys': 3.578125, 'used_cpu_user': 23.96875, 'used_cpu_sys_children': 0.0, 'used_cpu_user_children': 0.0, 'cluster_enabled': 0, 'db0': {'keys': 1, 'expires': 0, 'avg_ttl': 0}, 'db1': {'keys': 5, 'expires': 0, 'avg_ttl': 0}, 'db3': {'keys': 1, 'expires': 0, 'avg_ttl': 0}}
标签:aof,used,last,python,demo,redis,mem,memory
From: https://www.cnblogs.com/yangyucai/p/17640678.html