当我尝试在函数内部使用代码时,它不起作用,Python

When i try to use the code inside a function, it doesn´t work, Python

该凭证是关于实时使用我的带宽。该代码本身有效,但是当我尝试从函数中获取 return 数据时,它会中断,并且 return 错误数据。

感谢您的帮助。

这是我的代码,没有加入函数:

import psutil
import time

"""FIRST REFERENCE VALUES"""
last_recv = psutil.net_io_counters().bytes_recv
last_sent = psutil.net_io_counters().bytes_sent
last_total = last_recv + last_sent

while True:

    """CURRENT VALUES"""
    bytes_recv = psutil.net_io_counters().bytes_recv
    bytes_sent = psutil.net_io_counters().bytes_sent
    bytes_total = bytes_recv + bytes_sent

    """ DIFFERENCE BETWEEN ACTUAL - LAST_REFERENCE """
    new_recv = bytes_recv - last_recv
    new_sent = bytes_sent - last_sent
    new_total = bytes_total - last_total

    """ CHANGE OF UNIT """
    mb_new_recv = new_recv / 1024
    mb_new_sent = new_sent / 1024
    mb_new_total = new_total / 1024

    print(
        f"{mb_new_recv:.2f} MB recv, {mb_new_sent:.2f} MB sent,",
        f"{mb_new_total:.2f} MB total",
    )

    """NEW REFERENCE"""
    last_recv = bytes_recv
    last_sent = bytes_sent
    last_total = bytes_total

    time.sleep(1)

当我把它变成一个简单的函数时,它无法正常工作:

import time
import psutil


def Bandwidth():

    """FIRST REFERENCE VALUES"""
    last_recv = psutil.net_io_counters().bytes_recv
    last_sent = psutil.net_io_counters().bytes_sent
    last_total = last_recv + last_sent

    """CURRENT VALUES"""
    bytes_recv = psutil.net_io_counters().bytes_recv
    bytes_sent = psutil.net_io_counters().bytes_sent
    bytes_total = bytes_recv + bytes_sent

    """ DIFFERENCE BETWEEN ACTUAL - LAST_REFERENCE """
    new_recv = bytes_recv - last_recv
    new_sent = bytes_sent - last_sent
    new_total = bytes_total - last_total

    """ CHANGE OF UNIT """
    mb_new_recv = new_recv / 1024
    mb_new_sent = new_sent / 1024
    mb_new_total = new_total / 1024

    string = f"{mb_new_recv:.2f} MB recv, {mb_new_sent:.2f} MB sent, {mb_new_total:.2f} MB total"

    """NEW REFERENCE"""
    last_recv = bytes_recv
    last_sent = bytes_sent
    last_total = bytes_total

    time.sleep(1)
    return string


while True:
    a = Bandwidth()
    print(a)


像这样尝试一下,看看是否适合你...我所做的小改动都有注释。

import psutil
import time


def Ancho_timepo_real():

    """Ref values"""
    ultimo_recibido = psutil.net_io_counters().bytes_recv
    ultimo_enviado = psutil.net_io_counters().bytes_sent
    ultimo_total = ultimo_recibido + ultimo_enviado

    while True:
        """Actual Values"""
        bytes_recibido = psutil.net_io_counters().bytes_recv
        bytes_enviado = psutil.net_io_counters().bytes_sent
        bytes_total = bytes_recibido + bytes_enviado

        """ Difference Actual - Reference """
        new_recibido = bytes_recibido - ultimo_recibido
        new_enviado = bytes_enviado - ultimo_enviado
        new_total = bytes_total - ultimo_total

        """ Unit """
        mb_new_recibido = new_recibido / 1024
        mb_new_enviado = new_enviado / 1024
        mb_new_total = new_total / 1024
        string = (f"{mb_new_recibido:.2f} KB recibidos, {mb_new_enviado:.2f} "
                  f"KB enviados, {mb_new_total:.2f} KB totales") 
        print(string)  # removed the longer print statement that was identical to above
        """New ref"""
        ultimo_recibido = bytes_recibido
        ultimo_enviado = bytes_enviado
        ultimo_total = bytes_total

        time.sleep(1)

        yield string  # changed to yield instead of return


a = Ancho_timepo_real()
while True:
   print(next(a))