I want to synchronize data from between coroutines and I end up with a method not being called whenever there is "yield" inside it.
To be more precise, when I implement a DatagramProtocol class with the method datagram_received as per the doc (inspired from this), everything works fine, I receive the data. As soon as I add a "yield" inside the method datagram_received, the method is never called anymore. Here is an example:
loop = asyncio.get_event_loop()
lock = asyncio.Lock(loop=loop)
class MyProtocol(asyncio.DatagramProtocol):
def datagram_received(self, data, addr):
global my_data, lock
print("here")
# uncomment the following lines and datagram_received is
# not called at all (never see the "here" on the console)
#yield from lock
#try:
# my_data = float(data.decode())
#finally:
# lock.release()
loop.run_until_complete(loop.create_datagram_endpoint(MyProtocol, sock=create_socket(10000)))
loop.run_forever()
How can a method suddenly get not being called depending on the content of the method?
What am I missing? How the synchronization should be done?