I have two threads: one of them writes into a datastructre and the other monitors for changes using a while True loop.
Writer thread (Socket):
if new_mess:
datastruct.update(..)
Reader thread:
initial_state = datastruct.get_state()
while True:
if datastruct.get_state() != initial_state:
do_sth()
The problem is that the reader blocks the writer, resulting in poor performance on the writer side. I would like to create a coroutine on the method datastruct.curr_state so it doesn't block the writer. However, I am not sure how the await
condition should be structured.
Datastruct class:
class Datastruct:
...
async def get_state(self, key):
await asyncio.sleep(1)
return self.elements.get(key)
...
The above method gives cedes the execution context and then the event loop moves ahead with other awaiting tasks. Clearly, this is not a good approach since the coroutine waits until the sleep useless function is executed.
At this point, you may have realized that I'm completely new to asyncio
. Idk if there is an easier way of doing this non-blocking read. Any help will be greatly welcomed. Thanks!