How i can increase speed of this loop python?

182 Views Asked by At

I'm new to python programming and I'm trying to analyze the pending blocks of the BSC network. My program checks for pending blocks (event) and does something. The point is that a lot of events are happening while the loop event is active and my process is so slow to keep all new data being analised on real time.

If i remove the function hash_analise() and print all events is ok, the program is receiving data faster and i can print all hashs in realtime but when i call this function my program became slower.

I tried with threading but i need to syncronize all data from events and wait with thread.join() but when i wait this thread make slower than before.

Is any way to run this faster?

Thanks for help, code without thread:

def hash_analise(hash):

    try:
        hash_analise = web3.eth.get_transaction(hash)
        print_hash = Web3.toJSON(hash_analise)
        print("IMPRIME HASH1:", print_hash)

        if TOKEN_LOWER_CORRIGIDO in print_hash:
            print("\nCONTÉM A STRING ESCOLHIDA")

    except:
        print("TRANSAÇÃO NÃO LOCALIZADA")


if __name__ == "__main__":

    tx_filter = web3.eth.filter('pending')
    count = 0

    while True:

        for event in tx_filter.get_new_entries():
            evento = Web3.toJSON(event)

            txnhash = evento[1:67]

            hash_analise(txnhash)

            count += 1
            print("Main", count)
0

There are 0 best solutions below