Python asyncio dictionary ¿Queue? ¿Container?
Sometimes we just need some kind of queue based on a dictionary and a method to wait until the desired key is inserted. Something like:
dq = DictQueue() # ¿Queue? ¿Container? ¿Other? x = dq.get('magic key') y = dq.get_timeout('magic key', timeout=10) # seconds
but asyncio.Queue works like a list.
My use case: I'm working on an asterisk manager script which heavely depends on asyncio and need to handle more than one command or event at a time from different clients, max 20, connected to it. Doing it synchronous it's not an option, since wait for each command to end before send another one could end in a lot of of time, well, a lot of seconds. I uploaded my solution to github because it would be nice if other people could send pull requests of PEP8 correction, new methods: clear() for example, etc. I use it like this, the keys are actions id from asterisk, the vale, a dictionary, which is the result event from asterisk manager. This time, I will paste here the link to the code instead of paste it directly in this blog.
https://github.com/grizmio/DictQueue/blob/master/DictQueue.py















