-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
protocols/pipe_ipc: implement AsyncioChildComm for Unix (affected by p…
1 parent
8befc6a
commit dcea678
Showing
2 changed files
with
74 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,81 @@ | ||
import unittest | ||
import sys | ||
import asyncio | ||
import os | ||
|
||
from artiq.protocols import pipe_ipc | ||
|
||
|
||
class IPCCase(unittest.TestCase): | ||
def setUp(self): | ||
self.loop = asyncio.new_event_loop() | ||
if os.name == "nt": | ||
self.loop = asyncio.ProactorEventLoop() | ||
else: | ||
self.loop = asyncio.new_event_loop() | ||
asyncio.set_event_loop(self.loop) | ||
|
||
def tearDown(self): | ||
self.loop.close() | ||
|
||
async def _coro_test(self, child_blocking): | ||
async def _coro_test(self, child_asyncio): | ||
ipc = pipe_ipc.AsyncioParentComm() | ||
await ipc.create_subprocess(sys.executable, | ||
sys.modules[__name__].__file__, | ||
str(child_asyncio), | ||
ipc.get_address()) | ||
for i in range(10): | ||
ipc.write("{}\n".format(i).encode()) | ||
await ipc.drain() | ||
s = (await ipc.readline()).decode() | ||
self.assertEqual(int(s), i+1) | ||
ipc.write(b"-1\n") | ||
await ipc.process.wait() | ||
ipc.close() | ||
|
||
def test_blocking(self): | ||
self.loop.run_until_complete(self._coro_test(False)) | ||
|
||
def test_asyncio(self): | ||
self.loop.run_until_complete(self._coro_test(True)) | ||
|
||
|
||
def run_child(): | ||
child_comm = pipe_ipc.ChildComm(sys.argv[1]) | ||
def run_child_blocking(): | ||
child_comm = pipe_ipc.ChildComm(sys.argv[2]) | ||
while True: | ||
x = int(child_comm.readline().decode()) | ||
if x < 0: | ||
break | ||
child_comm.write((str(x+1) + "\n").encode()) | ||
child_comm.close() | ||
|
||
|
||
async def coro_child(): | ||
child_comm = pipe_ipc.AsyncioChildComm(sys.argv[2]) | ||
await child_comm.connect() | ||
while True: | ||
x = int((await child_comm.readline()).decode()) | ||
if x < 0: | ||
break | ||
child_comm.write((str(x+1) + "\n").encode()) | ||
await child_comm.drain() | ||
child_comm.close() | ||
|
||
|
||
def run_child_asyncio(): | ||
if os.name == "nt": | ||
loop = asyncio.ProactorEventLoop() | ||
asyncio.set_event_loop(loop) | ||
else: | ||
loop = asyncio.get_event_loop() | ||
loop.run_until_complete(coro_child()) | ||
loop.close() | ||
|
||
|
||
def run_child(): | ||
if sys.argv[1] == "True": | ||
run_child_asyncio() | ||
else: | ||
run_child_blocking() | ||
|
||
if __name__ == "__main__": | ||
run_child() |