I am using asyncio on Windows and have a reference to a transport object of a named pipe:
class DataPipeHandler(asyncio.Protocol):
def connection_made(self, trans):
self.trans = trans # <<== this is a reference to a transport object of type _ProactorDuplexPipeTransport
loop = asyncio.get_event_loop()
server = loop.start_serving_pipe(lambda: DataPipeHandler(), r'\\.\pipe\test-pipe')
now I would like to use self.trans to synchronously write and then read data from the named pipe. How can I do this?
Its important for me to do this synchronously as this is kind of RPC call I am doing using the pipe (writing something and getting back response quickly) and I do want to block all the other activities of the even loop until this "pipe RPC call" returns. If I don't block all other activities of the event loop until this RPC call is done I will have unwanted side effects as the loop will continue to process other events I don't want it to process yet.
What I want to do (the write to pipe and then read) is very similar to someone who is calling urllib2.urlopen(urllib2.Request('http://www.google.com')).read()
from the event loop thread - here too all the event loop activities will be blocked until we get response from a remote http server.
I know that I can call self.trans.write(data) but this does not write the data synchronously (as I understand it does not block)
Thanks.
EDIT: Following the first comment let me add:
I understand I should never block event loop and that I can use synchronization primitives for accomplishing what I want. But lets say you have event loop that is doing 10 different activities in parallel and one of them is doing some kind of RPC (as describe above) and all other 9 activities should be blocked until this RPC is done. so I have 2 options:
(1) add synchronization primitives (lock/semaphore/condition) as you suggested to all these 10 activities for synchronizing them.
(2) implement this RPC by blocking write and then blocking read from/to the pipe. (assuming I trust the other side of the pipe)
I know this is not the usual way of using event loops but in my specific case I think (2) is better. (simpler logic)