From 25852794a81ea23b5220432167c3282fa366e908 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Wed, 11 Jul 2018 18:08:57 -0400 Subject: [PATCH] Move chan connect helper to ipc mod --- tractor/ipc.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tractor/ipc.py b/tractor/ipc.py index 7bc647c..56c09db 100644 --- a/tractor/ipc.py +++ b/tractor/ipc.py @@ -5,6 +5,7 @@ from typing import Coroutine, Tuple import msgpack import trio +from async_generator import asynccontextmanager from .log import get_logger log = get_logger('ipc') @@ -189,3 +190,14 @@ class Channel: def connected(self): return self.squeue.connected() if self.squeue else False + + +@asynccontextmanager +async def _connect_chan(host, port): + """Create and connect a channel with disconnect on + context manager teardown. + """ + chan = Channel((host, port)) + await chan.connect() + yield chan + await chan.aclose()