Linux中Python程序CPU占用高排查

python如何指定使用的cpu核_Python如何利用多核处理器 python多线程不能利用多核cpu,但有时候多线程确实比单线程快。python 为什么不能利用多核CPU GIL其实是因为在python中有一个GIL(Global Interpreter Lock),中文为:全局解释器锁. 1.最开始时候设计GIL是 ...python学习笔记(二十九)为什么python的多线程不能利用多核CPU问题:为什么python的多线程不能利用多核CPU... 阅读详情

Linux中Python程序CPU占用高排查,Linux中Python程序CPU占用高排查,Linux中Python程序CPU占用高排查

 

kafka-python==2.0.2和 gevent 新版本 生产机器中发现CPU占用极高,应该是有bug:https://github.com/dpkp/kafka-python/issues/2168 。目前推测是gevent patch后Kafka Consumer 和 heartbeat 占用了大量的 CPU 。

机器环境

  • Debian 10
  • Python 3.7.3
  • kafka-python 2.0.2
  • gevent 21.12.0 (greenlet 1.1.2)

测试代码

<span style="color:#333333"><span style="background-color:#f6f6f6"><span style="color:#444444"><span style="background-color:#f6f6f6"><span style="color:#888888"># vim cppla.py</span>
<span style="color:#333333"><strong>from</strong></span> gevent <span style="color:#333333"><strong>import</strong></span> monkey
<span style="color:#333333"><strong>from</strong></span> gevent.pywsgi <span style="color:#333333"><strong>import</strong></span> WSGIServer
monkey.patch_all()
 
<span style="color:#333333"><strong>from</strong></span> multiprocessing <span style="color:#333333"><strong>import</strong></span> cpu_count, Process
<span style="color:#333333"><strong>from</strong></span> flask <span style="color:#333333"><strong>import</strong></span> Flask, jsonify

<span style="color:#333333"><strong>from</strong></span> kafka <span style="color:#333333"><strong>import</strong></span> KafkaConsumer, KafkaProducer
<span style="color:#333333"><strong>from</strong></span> kafka.errors <span style="color:#333333"><strong>import</strong></span> NoBrokersAvailable, KafkaTimeoutError

app = Flask(__name__)
 
<span style="color:#1f7199">@app.route("/cppla", methods=['GET'])</span>
<span style="color:#333333"><strong>def</strong></span> <span style="color:#880000"><strong>function_benchmark</strong></span>():
    <span style="color:#333333"><strong>return</strong></span> jsonify(
        {
            <span style="color:#880000">"status"</span>: <span style="color:#880000">"ok"</span>,
        }
    ), <span style="color:#880000">200</span>
 
<span style="color:#333333"><strong>def</strong></span> <span style="color:#880000"><strong>run</strong></span>():
    mulserver = WSGIServer((<span style="color:#880000">'0.0.0.0'</span>, <span style="color:#880000">8080</span>), app)
    mulserver.start()
 
    <span style="color:#333333"><strong>def</strong></span> <span style="color:#880000"><strong>server_forever</strong></span>():
        mulserver.start_accepting()
        mulserver._stop_event.wait()
 
    <span style="color:#333333"><strong>for</strong></span> i <span style="color:#333333"><strong>in</strong></span> range(cpu_count()):
        p = Process(target=server_forever)
        p.start()

KAFKA_URI = {
    <span style="color:#880000">"BOOTSTRAP_SERVERS"</span>: [
        <span style="color:#880000">'192.168.1.2:9092'</span>,
        <span style="color:#880000">'192.168.1.3:9092'</span>,
        <span style="color:#880000">'192.168.1.4:9092'</span>
    ],
    <span style="color:#880000">"TOPIC"</span>: <span style="color:#880000">"test"</span>,
    <span style="color:#880000">"GROUP_ID"</span>: <span style="color:#880000">"v1"</span>,
    <span style="color:#880000">"KEY"</span>: <span style="color:#880000">"order"</span>
}

<span style="color:#333333"><strong>class</strong></span> <span style="color:#880000"><strong>kafkaClient</strong></span>(object):

    <span style="color:#333333"><strong>def</strong></span> <span style="color:#880000"><strong>__init__</strong></span>(self):
        print(<span style="color:#880000">"init start "</span>)
        self._producer_client = self._createProducer
        self._consumer_client = self._createConsumer
        print(<span style="color:#880000">"init end "</span>)

<span style="color:#1f7199">    @property</span>
    <span style="color:#333333"><strong>def</strong></span> <span style="color:#880000"><strong>_createProducer</strong></span>(self):
        <span style="color:#333333"><strong>try</strong></span>:
            <span style="color:#333333"><strong>return</strong></span> KafkaProducer(
                bootstrap_servers=KAFKA_URI[<span style="color:#880000">"BOOTSTRAP_SERVERS"</span>],
                retries=<span style="color:#880000">3</span>
            )
        <span style="color:#333333"><strong>except</strong></span> NoBrokersAvailable:
            print(<span style="color:#880000">"bo brokers"</span>)

<span style="color:#1f7199">    @property</span>
    <span style="color:#333333"><strong>def</strong></span> <span style="color:#880000"><strong>_createConsumer</strong></span>(self):
        <span style="color:#333333"><strong>try</strong></span>:
            <span style="color:#333333"><strong>return</strong></span> KafkaConsumer(
                KAFKA_URI[<span style="color:#880000">"TOPIC"</span>],
                group_id=KAFKA_URI[<span style="color:#880000">"GROUP_ID"</span>],
                bootstrap_servers=KAFKA_URI[<span style="color:#880000">"BOOTSTRAP_SERVERS"</span>],
                auto_offset_reset=<span style="color:#880000">"latest"</span>,
                enable_auto_commit=<span style="color:#333333"><strong>True</strong></span>,
                auto_commit_interval_ms=<span style="color:#880000">5000</span>,
            )
        <span style="color:#333333"><strong>except</strong></span> NoBrokersAvailable:
            print(<span style="color:#880000">"no brokers"</span>)

<span style="color:#1f7199">    @property</span>
    <span style="color:#333333"><strong>def</strong></span> <span style="color:#880000"><strong>consumer</strong></span>(self):
        print(<span style="color:#880000">"consumer function"</span>)
        <span style="color:#333333"><strong>try</strong></span>:
            <span style="color:#333333"><strong>for</strong></span> x <span style="color:#333333"><strong>in</strong></span> self._consumer_client:
                <span style="color:#333333"><strong>yield</strong></span> {
                    <span style="color:#880000">"partition"</span>: x.partition,
                    <span style="color:#880000">"timestamp"</span>: x.timestamp,
                    <span style="color:#880000">"offset"</span>: x.offset,
                    <span style="color:#880000">"value"</span>: x.value.decode()
                }
        <span style="color:#333333"><strong>except</strong></span> Exception <span style="color:#333333"><strong>as</strong></span> e:
            print(e)


    <span style="color:#333333"><strong>def</strong></span> <span style="color:#880000"><strong>producer</strong></span>(self, msg):
        print(<span style="color:#880000">"consumer function"</span>)
        <span style="color:#333333"><strong>if</strong></span> <span style="color:#333333"><strong>not</strong></span> self._producer_client:
            print(<span style="color:#880000">"mark0"</span>)
            <span style="color:#333333"><strong>return</strong></span> <span style="color:#333333"><strong>False</strong></span>
        <span style="color:#333333"><strong>else</strong></span>:
            <span style="color:#333333"><strong>try</strong></span>:
                pre = datetime.datetime.now()
                self._producer_client.send(
                    topic=KAFKA_URI[<span style="color:#880000">"TOPIC"</span>],
                    key=KAFKA_URI[<span style="color:#880000">"KEY"</span>].encode(),
                    value=msg.encode()
                ).add_callback(self.on_send_success).add_errback(self.on_send_error)
                next = datetime.datetime.now()
                <span style="color:#333333"><strong>if</strong></span> (next-pre).seconds > <span style="color:#880000">60</span>:
                    print(<span style="color:#880000">"60s"</span>)
                <span style="color:#333333"><strong>return</strong></span> <span style="color:#333333"><strong>True</strong></span>
            <span style="color:#333333"><strong>except</strong></span> KafkaTimeoutError <span style="color:#333333"><strong>as</strong></span> e:
                print(<span style="color:#880000">"timeout"</span>)
            <span style="color:#333333"><strong>except</strong></span> Exception <span style="color:#333333"><strong>as</strong></span> e:
                print(<span style="color:#880000">"exception"</span>)
                <span style="color:#333333"><strong>return</strong></span> <span style="color:#333333"><strong>False</strong></span>

    <span style="color:#333333"><strong>def</strong></span> <span style="color:#880000"><strong>on_send_success</strong></span>(self, metadata):
        print(metadata)

    <span style="color:#333333"><strong>def</strong></span> <span style="color:#880000"><strong>on_send_error</strong></span>(self, excp):
        print(<span style="color:#880000">"excp"</span>)
kafka_client = kafkaClient()

<span style="color:#333333"><strong>if</strong></span> __name__ == <span
Linux服务器CPU占用过高排查与优化实战指南 CPU性能优化是Linux系统运维的核心技能之一。从计算机体系结构角度看,CPU作为计算资源的核心调度单元,其使用率直接反映系统健康状态。通过top、vmstat等工具可以分析用户态(%us)与内核态(%sy)的CPU时间分布,结合负载均衡(load average)指标判断瓶颈类型。在分布式系统和云原生场景下,合理使用perf、bpftrace等工具进行调用链分析,配合火焰图可视化技术热点,能够快速定位Java应用死循环、锁竞争或内核调度异常等问题。本文基于多年运维经验,系统梳理从快速止血到深度优化的全链 阅读详情

相关推荐

CPU性能问题排查实战:从定位高占用进程到系统优化

CPU性能优化是系统运维和开发中的核心议题,其本质在于高效管理计算资源。从原理上讲,操作系统通过进程调度分配CPU时间片,当某个进程异常占用大量时间片时,便会导致系统响应迟缓、资源耗尽。掌握CPU性能诊断技术,对于保障服务稳定性、提升用户体验具有重要价值。无论是服务器运维、开发环境调试,还是解决个人电脑卡顿,快速定位并分析高CPU占用进程都是关键技能。本文聚焦于实战,系统介绍在Windows与Linux环境下,如何运用任务管理器、资源监视器、top/htop、perf等工具链,精准定位如‘服务主机’异常、后

weixin_30882895的博客 281

CPU占用率100%排查指南:从根因分析到实战解决

CPU使用率是衡量计算资源负载的核心指标,它反映了处理器执行任务的时间比例。当CPU占用率达到100%时,意味着计算需求超过了处理能力,系统性能将严重下降。其背后原理涉及进程调度、资源竞争与硬件瓶颈等多个层面。从技术价值看,快速定位CPU瓶颈是保障系统稳定性和用户体验的关键技能,尤其在微服务、容器化等现代架构中更为重要。常见的应用场景包括服务器性能调优、应用故障排查以及云资源优化等。本文将结合火焰图、jstack等热词,系统解析CPU满载的软件与硬件根因,并提供从监控定位到深度诊断的完整方法论,涵盖Linu

weixin_30644369的博客 347

Arduino Nano + MCP3422A0 ADC 毫欧表:3档分辨率(0.1mΩ-1.3kΩ)开尔文连接实测

本文详细介绍了如何使用Arduino Nano与MCP3422A0 ADC构建高精度毫欧表,采用四线制开尔文连接和三档量程设计,实现0.1mΩ-1.3kΩ的精确测量。通过精密恒流源和18位ADC技术,结合智能量程切换和数字滤波算法,显著提升测量精度和稳定性,适用于电子维修、PCB测试等场景。

weixin_28292761的博客 323

Vivado2019.1 ZYNQ7020无Uart SDK调试打印调试信息xil_printf

Vivado2019.1 ZYNQ7020无Uart SDK调试打印调试信息xil_printf

泛红尘的博客 2995

【各种轮子2】基于Icepak的PCB散热仿真比较

随着电子产品的集成度越来越高,PCB板的尺寸越来越小,板级芯片散热的问题越来越成为电子工程师的一个重要挑战。对于板级芯片散热,主要依靠工程师对PCB自身的设计进行优化,同时要兼顾系统的尺寸和成本。本文分别从芯片角度和PCB角度进行建模,芯片模型选用QFN封装,PCB模型采用走线导入模型,探讨了芯片结构,PCB铜厚,PCB叠层厚度对芯片散热的影响。

3668

MAX30102传感器实战:从寄存器配置到Arduino心率血氧监测

本文详细介绍了MAX30102传感器在Arduino平台上的实战应用,从寄存器配置到心率血氧监测的全过程。通过清晰的代码示例和实用技巧,帮助开发者快速实现脉搏血氧仪功能,包括I2C通信、FIFO数据处理及算法实现,适用于健康监测和可穿戴设备开发。

weixin_29182481的博客 511

win10安装openvpn客户端

进入C:\Program Files\TAP-Windows\bin目录,然后右键用管理员权限执行addtap.bat文件,此文件执行会创建一个网卡。链接:https://pan.baidu.com/s/1KAifTiP7qp1i5ZkfdnfwkA。4.2 进入安装目录的config下新建一个文件夹,将证书文件全部挪进去(不移动.ovpn文件)3.4 将拿到的ssl认证文件放入安装目录的config目录下。然后修改ovpn文件 “./”代表当前路径,第二个链接重复。3.1 一直下一步到这里打上勾。

qq_36200932的博客 3万+

股票行情接口,最全的接入方法

股票行情接口一般有两种,一种是WebSocket,一种是http,通过WebSocket可以获取逐笔的股票tick数据,实时并且高效;通过http接口可以获取股票行情接口中的股票历史数据;WebSocket是一种高效的双向通信协议,它允许数据的实时推送,避免了不断的轮询请求。其次,WebSocket允许您订阅特定的数据源或股票数据产品,只接收您感兴趣的信息,提高了数据的效率和可用性。通过接入股票行情数据的HTTP接口,您可以将数据导入到自己的分析工具或系统中,进行更深入的数据分析、模型构建和决策支持。

apifinance的博客 3423

RT-DETR-R18 上下文信息融合:引入全局特征提升遮挡目标检测

"""轻量化全局特征提取器(2层Transformer编码器)"""# 特征投影层(将多尺度特征统一维度)# 轻量化Transformer编码器(分组卷积优化)# 全局特征池化(输出 [bs, hidden_dim])# local_features: 多尺度特征列表 [C3, C4, C5],各 [bs, c, h, w]# 统一投影至 hidden_dim 并展平为序列# Transformer编码全局上下文# 全局池化得到全局向量。

走向CTO的路上... 494

A*寻路算法的最简单通俗讲解

为什么我的眼里常含泪水?因为我有一个算法不会。为了节约点眼泪,今天我们就来介绍著名的A*寻路算法(或简称A*算法)。 Peter Hart, Nils Nilsson 和 Bertram Raphael 在1968年首次提出了著名的A*算法。A*算法其实是Dijkstra最短路径算法的一种扩展。不一样的是,在A*算法中我们需要使用Heuristics 来导引搜索过程,以期达到更好的表现。 ...

白马负金羁 2万+

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

上一篇: 懒人必备神器 ,一个创意十足的 Python 命令行工具
下一篇: 人人都能学会的 Python 多线程指南
Python_xiaowu
博客等级 码龄5年 3096粉丝 178原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值