0

I'm pulling data from my websocket server (Geth node) that returns an dictionary. I iterate over this dictionary in order to process data from it. My issue is the following:

First iteration would pull a dictionary containing

A B C D

Second iteration would pull a dictionary containing

E A C D B F

How would I implement my code in a way that streams data continuously, instead of having to receive new data along with already processed one? With the previous example, the second iteration of my code should return only E and F

Here's my code

def liquidCheck(self, address):
        methodTarget = "0xf305d719" 
        while True:
            tx_data = self.w3.geth.txpool.content()["pending"] #This function calls my WebSocket server and returns the dictionary
            try:
                for k, v in tx_data.items():
                    for k2, v2 in v.items():
                        methodId = v2.input[0:10]
                        if methodId.lower() != methodTarget.lower():
                            continue
                        else:
                            print("Target found, ciao")
                            return
            except:
                print(traceback.format_exc()) 
  • I guess one way is to delete the items from the dictionary, another option I just thought of would be to have some kind of key like `"sent": False` that would be set to True when the contents get sent and then could use some condition to not send what already is sent. so sth like this: `dct = {{"message": "stuff", "sent": False}, {"message": "hello", "sent": True}}` and so You would iterate over this and check if `"sent"` is False and if it is then send the message, otherwise skip or sth – Matiiss Jul 11 '21 at 21:17
  • Thanks for your response and your time @Matiiss, but either options wouldn't suffice since on each iteration the dictionary is declared again. – Bernardo García Jul 11 '21 at 21:25
  • how about having another dictionary that stores this info? – Matiiss Jul 11 '21 at 21:27
  • @Matiiss I thought of this, but any identifier I could use would be inside v2, so I'd have to iterate over these values twice, which would kill a lot of time. Getting new data as it is fetched by the webserver is very important with this script. Also, sorry for my bad english – Bernardo García Jul 11 '21 at 21:29
  • no really, just store everything that gets sent in a list or dictionary and then next time just check `if to_send in already_sent:`, wouldn't that work? – Matiiss Jul 11 '21 at 21:35
  • @Matiiss Oh, I understand what you mean. Still, the issue would be that this dictionary has tons of values in it (Sometimes in the thousands) and iterating over those values would take time. I've been reading Asyncio may be a solution but I don't know how to implement it in this case – Bernardo García Jul 11 '21 at 21:45
  • could also try using `next` or sth, I also don't have a clear understanding of how the dictionary is created and sent because if You send multiple data then it seems that You iterate over the dictionary which would allow to use `next` or sth, otherwise I have no idea – Matiiss Jul 11 '21 at 21:51

0 Answers0