Function returns value only the first time it is executed

Asked

Viewed 62 times

0

This is my code:

import conector_modbus as conector_modbus
import leitor_csv as leitor_csv

address_file = 'address.csv'
dados = leitor_csv.leitor(address_file,"dicionario")

def coletar_dados():
    for dado in dados:
        if dado['var'] == "int":
            dado['valor'] = conector_modbus.ler_dado(int(dado['reg']),"int")
            yield dado
        if dado['var'] == "float":
            dado['valor'] = conector_modbus.ler_dado(int(dado['reg']),"float")
            yield dado

def preparar_dados(dados_modbus,maquina,num):
    registradores = {}
    for dado in dados_modbus:
        if dado['maquina'] == maquina and dado['num'] == num:
            registradores.update({dado['nome']:dado['valor']})
    return registradores


if conector_modbus.status_modbus("10.123.1.5",502) == True:
    dados_modbus = coletar_dados()
    dados_br1 = preparar_dados(dados_modbus,"br","1")
    print(dados_br1)

    dados_br1 = preparar_dados(dados_modbus,"br","1")
    print(dados_br1)

And this is the result:

root@jonatas-A530:/media/jonatas/Documentos/Projetos/orange_modbus# python3 teste.py
{'producao_ton': 1056.0025634765625, 'fluxo_ton_h': 0, 'corrente_amp': 32767, 'horimetro_h': 1064.68994140625, 'tempo_sob_carga_h': 4.1541666984558105, 'tempo_ocioso_h': 0.466552734375}
{}

The function was called 2 times, but in the second it does not return value. I would like to understand why this occurs.

3 answers

2

Its function coletar_dados() is a generator, that is, after all the data has been consumed its purpose has been fulfilled.

To use it again you need another instance of Generator.

In your case just create a Generator new before using it again.

if conector_modbus.status_modbus("10.123.1.5",502) == True:
    dados_modbus = coletar_dados()
    dados_br1 = preparar_dados(dados_modbus,"br","1")
    print(dados_br1)

    dados_modbus = coletar_dados()  # criando outro generator
    dados_br1 = preparar_dados(dados_modbus,"br","1")
    print(dados_br1)
  • Hello Fernando, I called again the function collectar_data() according to your tip, but I keep getting the same result, I may be missing something else?

  • Probably his leitor_csv is also a Generator and needs to be started again. As I do not know the code of this module I can not be sure, but it is most likely.

  • There is no way to "reset" Generator?

  • Just call leitor_csv.leitor(address_file,"dicionario") again.

0


Jonp, tries to make a loop in his function to collect data, because in it you, make a Yield in data, it is with Yield your function becomes interable, having the need to interact in it to obtain the data. Try this:

if conector_modbus.status_modbus("10.123.1.5",502) == True:
    dados_modbus = [ dados for dados in coletar_dados()]
    dados_br1 = preparar_dados(dados_modbus,"br","1")
    print(dados_br1)

    dados_br1 = preparar_dados(dados_modbus,"br","1")
    print(dados_br1)
  • This would be the same as: data_modbus = list(collect_data())?

  • Yes I would do the same.

  • Only to log that when converting a Generator for a list, you are loading all the content in memory. If coletar_dados() return 10GB of data, you will be putting everything in memory, and killing the purpose of using generators.

0

Performing a survey I found the following link

One of the answers was the following:

  • Store Generator results in a data structure in memory or disk that you can iterate again:

    dados_modbus = list(coletar_dados())
    for dado in dados_modbus: print(dado)
    for dado in dados_modbus: print(dado)`
    

I will use this solution, although the other previous suggestions are correct as well.

  • Only to log that when converting a Generator for a list, you are loading all the content in memory. If coletar_dados() return 10GB of data, you will be putting everything in memory, and killing the purpose of using generators.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.