python - How to perform *synchronous* read/write into/from a asyncio transport object -
i using asyncio on windows , have reference transport object of named pipe:
class datapipehandler(asyncio.protocol): def connection_made(self, trans): self.trans = trans # <<== reference transport object of type _proactorduplexpipetransport loop = asyncio.get_event_loop() server = loop.start_serving_pipe(lambda: datapipehandler(), r'\\.\pipe\test-pipe')
now use self.trans synchronously write , read data named pipe. how can this?
its important me synchronously kind of rpc call doing using pipe (writing , getting response quickly) , want block other activities of loop until "pipe rpc call" returns. if don't block other activities of event loop until rpc call done have unwanted side effects loop continue process other events don't want process yet.
what want (the write pipe , read) similar calling urllib2.urlopen(urllib2.request('http://www.google.com')).read()
event loop thread - here event loop activities blocked until response remote http server.
i know can call self.trans.write(data) not write data synchronously (as understand not block)
thanks.
edit: following first comment let me add:
i understand should never block event loop , can use synchronization primitives accomplishing want. lets have event loop doing 10 different activities in parallel , 1 of them doing kind of rpc (as describe above) , other 9 activities should blocked until rpc done. have 2 options:
(1) add synchronization primitives (lock/semaphore/condition) suggested these 10 activities synchronizing them.
(2) implement rpc blocking write , blocking read from/to pipe. (assuming trust other side of pipe)
i know not usual way of using event loops in specific case think (2) better. (simpler logic)
i think have use threading synchronization primitives make sure whole loop (current thread) blocked. think best offer using threading queue , join features.
Comments
Post a Comment