首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >在将RxPy从1.x升级到3.x后,不要订阅工作

在将RxPy从1.x升级到3.x后,不要订阅工作
EN

Stack Overflow用户
提问于 2019-04-17 04:06:17
回答 1查看 321关注 0票数 0

我正在使用Python3.7.3。

我尝试将RxPy从1.6.1 (1.x)升级到3.0.0a3 (3.x)。

使用 1.x的RxPy旧代码

代码语言:javascript
代码运行次数:0
运行
复制
from rx import Observable
import psutil
import numpy as np
import pylab as plt

cpu_data = (Observable
            .interval(100)  # Each 100 milliseconds
            .map(lambda x: psutil.cpu_percent())
            .publish())
cpu_data.connect()


def monitor_cpu(npoints):
    lines, = plt.plot([], [])
    plt.xlim(0, npoints)
    plt.ylim(0, 100)

    cpu_data_window = cpu_data.buffer_with_count(npoints, 1)

    def update_plot(cpu_readings):
        lines.set_xdata(np.arange(len(cpu_readings)))
        lines.set_ydata(np.array(cpu_readings))
        plt.draw()

    alertpoints = 4
    high_cpu = (cpu_data
                .buffer_with_count(alertpoints, 1)
                .map(lambda readings: all(r > 20 for r in readings)))

    label = plt.text(1, 1, "normal")

    def update_warning(is_high):
        if is_high:
            label.set_text("high")
        else:
            label.set_text("normal")

    high_cpu.subscribe(update_warning)
    cpu_data_window.subscribe(update_plot)

    plt.show()


if __name__ == '__main__':
    monitor_cpu(10)

如果您运行代码,您可以看到一个实时的CPU监视器图表。

但是,在我安装了新的RxPy之后

代码语言:javascript
代码运行次数:0
运行
复制
pip3 install --pre rx

使用下面的新代码,它只显示白色的,没有任何动态图表。

而函数update_plot实际上从未运行过。有什么想法吗?

使用 3.x的RxPy新代码

代码语言:javascript
代码运行次数:0
运行
复制
from rx import interval, operators as op
import psutil
import numpy as np
import pylab as plt


cpu_data = interval(100).pipe(  # Each 100 milliseconds
    op.map(lambda x: psutil.cpu_percent()),
    op.publish())
cpu_data.connect()


def monitor_cpu(npoints):
    lines, = plt.plot([], [])
    plt.xlim(0, npoints)
    plt.ylim(0, 100)

    cpu_data_window = cpu_data.pipe(
        op.buffer_with_count(npoints, 1))

    def update_plot(cpu_readings):
        print('update')  # here never runs
        lines.set_xdata(np.arange(len(cpu_readings)))
        lines.set_ydata(np.array(cpu_readings))
        plt.draw()

    alertpoints = 4
    high_cpu = cpu_data.pipe(
                op.buffer_with_count(alertpoints, 1),
                op.map(lambda readings: all(r > 20 for r in readings)))

    label = plt.text(1, 1, "normal")

    def update_warning(is_high):
        if is_high:
            label.set_text("high")
        else:
            label.set_text("normal")

    high_cpu.subscribe(update_warning)
    cpu_data_window.subscribe(update_plot)

    plt.show()


if __name__ == '__main__':
    monitor_cpu(10)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-17 05:33:32

时间单位现在以秒为单位

代码语言:javascript
代码运行次数:0
运行
复制
cpu_data = interval(0.1).pipe(  # Each 100 milliseconds
    op.map(lambda x: psutil.cpu_percent()),
    op.publish())
cpu_data.connect()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55719712

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档