Python怎么实时获取任务请求对应的Nginx日志
Python怎么实时获取任务请求对应的Nginx日志
这篇文章主要讲解了“Python怎么实时获取任务请求对应的Nginx日志”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Python怎么实时获取任务请求对应的Nginx日志”吧!
需求描述
项目需求测试过程中,需要向Nginx服务器发送一些用例请求,然后查看对应的Nginx日志,判断是否存在特征内容,来判断任务是否执行成功。为了提升效率,需要将这一过程实现自动化。
实践环境
Python 3.6.5
代码设计与实现
#!/usr/bin/envpython#-*-coding:utf-8-*-"""@CreateTime:2021/06/269:05@Author:shouke"""importtimeimportthreadingimportsubprocessfromcollectionsimportdequedefcollect_nginx_log():globalnginx_log_queueglobalis_tasks_competeglobaltask_statusargs="tail-0f/usr/local/openresty/nginx/logs/access.log"whiletask_status!="req_log_got":withsubprocess.Popen(args,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True,universal_newlines=True)asproc:log_for_req=""outs,errs="",""try:outs,errs=proc.communicate(timeout=2)exceptsubprocess.TimeoutExpired:print("获取nginx日志超时,正在重试")proc.kill()try:outs,errs=proc.communicate(timeout=5)exceptsubprocess.TimeoutExpired:print("获取nginx日志超时,再次超时,停止重试")breakfinally:forlineinouts.split(""):flag=""client_ip":"10.118.0.77""#特征ifflaginline:#查找包含特征内容的日志log_for_req+=lineiftask_status=="req_finished":nginx_log_queue.append(log_for_req)task_status="req_log_got"defrun_tasks(task_list):"""运行任务:paramtask_list任务列表"""globalnginx_log_queueglobalis_tasks_competeglobaltask_statusfortaskintask_list:thread=threading.Thread(target=collect_nginx_log,name="collect_nginx_log")thread.start()time.sleep(1)#执行任务前,让收集日志线程先做好准备print("正在执行任务:%s"%task.get("name"))#执行Nginx任务请求#...task_status="req_finished"time_to_wait=0.1whiletask_status!="req_log_got":#请求触发的nginx日志收集未完成time.sleep(time_to_wait)time_to_wait+=0.01else:#获取到用例请求触发的nginx日志ifnginx_log_queue:nginx_log=nginx_log_queue.popleft()task_status="req_ready"#解析日志#dosomethinghere#...else:print("存储请求日志的队列为空")#dosomethinghere#...if__name__=="__main__":nginx_log_queue=deque()is_tasks_compete=False#所有任务是否执行完成task_status="req_ready"#req_ready,req_finished,req_log_got#存放执行次任务任务的一些状态print("###########################任务开始###########################")tast_list=[{"name":"test_task","other":"..."}]run_tasks(tast_list)is_tasks_compete=Truecurrent_active_thread_num=len(threading.enumerate())whilecurrent_active_thread_num!=1:time.sleep(2)current_active_thread_num=len(threading.enumerate())print("###########################任务完成###########################")
注意:
1、上述代码为啥不一步到位,直接tail -0f /usr/local/openresty/nginx/logs/access.log | grep "特征内容"
呢?这是因为这样做无法获取到Nginx的日志
2、实践时发现,第一次执行proc.communicate(timeout=2)
获取日志时,总是无法获取,会超时,需要二次获取,并且timeout
设置太小时(实践时尝试过设置为1秒
),也会导致第二次执行时无法获取Nginx日志。
感谢各位的阅读,以上就是“Python怎么实时获取任务请求对应的Nginx日志”的内容了,经过本文的学习后,相信大家对Python怎么实时获取任务请求对应的Nginx日志这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是恰卡编程网,小编将为大家推送更多相关知识点的文章,欢迎关注!
推荐阅读
-
Python 3.12 新特性解析:模式匹配增强与性能优化实战
-
Lightly IDE 深度评测:轻量级 Python 开发工具是否适合团队协作?
-
VS Code 自定义配置:JSON 文件修改、代码片段与任务自动化脚本
-
Python 虚拟环境选择:venv、conda、poetry 的适用场景对比
-
PyCharm+GitHub Copilot:Python 开发中 AI 辅助编码的最佳实践
-
PyCharm 无法识别虚拟环境?5 步排查 Python 解释器配置问题
-
数据科学工具链:Jupyter Notebook+RStudio+Python 的协同工作流
-
Python 3.12 新特性:模式匹配增强与性能改进实战
-
Lightly IDE 适合谁?轻量级 Python 开发工具深度评测
-
Python IDE 终极对比:PyCharm vs VS Code vs Jupyter Notebook