pythonpython-3.xfunctionbandwidthpsutil

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


The scrip is about getting the use of my bandwidth, real-time. The code works by itself, but when i try to return data from a function, it breaks, and return wrong data.

Thanks for your help.

This is my code without beeing in a funtion:

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)

And when i make it into a simple function it doesn´t work as it should:

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)



Solution

  • Try it like this and see if that works for you... There are notes for the minor changes I made.

    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))