Allow opening a portal through an existing channel
parent
90499c19d7
commit
96cf4b6e9f
|
@ -314,7 +314,8 @@ class LocalPortal:
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def open_portal(
|
async def open_portal(
|
||||||
channel: Channel,
|
channel: Channel,
|
||||||
nursery: Optional[trio.Nursery] = None
|
nursery: Optional[trio.Nursery] = None,
|
||||||
|
start_msg_loop: bool = True,
|
||||||
) -> typing.AsyncGenerator[Portal, None]:
|
) -> typing.AsyncGenerator[Portal, None]:
|
||||||
"""Open a ``Portal`` through the provided ``channel``.
|
"""Open a ``Portal`` through the provided ``channel``.
|
||||||
|
|
||||||
|
@ -332,15 +333,17 @@ async def open_portal(
|
||||||
if channel.uid is None:
|
if channel.uid is None:
|
||||||
await actor._do_handshake(channel)
|
await actor._do_handshake(channel)
|
||||||
|
|
||||||
msg_loop_cs: trio.CancelScope = await nursery.start(
|
msg_loop_cs = None
|
||||||
partial(
|
if start_msg_loop:
|
||||||
actor._process_messages,
|
msg_loop_cs: trio.CancelScope = await nursery.start(
|
||||||
channel,
|
partial(
|
||||||
# if the local task is cancelled we want to keep
|
actor._process_messages,
|
||||||
# the msg loop running until our block ends
|
channel,
|
||||||
shield=True,
|
# if the local task is cancelled we want to keep
|
||||||
|
# the msg loop running until our block ends
|
||||||
|
shield=True,
|
||||||
|
)
|
||||||
)
|
)
|
||||||
)
|
|
||||||
portal = Portal(channel)
|
portal = Portal(channel)
|
||||||
try:
|
try:
|
||||||
yield portal
|
yield portal
|
||||||
|
@ -352,6 +355,7 @@ async def open_portal(
|
||||||
await channel.send(None)
|
await channel.send(None)
|
||||||
|
|
||||||
# cancel background msg loop task
|
# cancel background msg loop task
|
||||||
msg_loop_cs.cancel()
|
if msg_loop_cs:
|
||||||
|
msg_loop_cs.cancel()
|
||||||
|
|
||||||
nursery.cancel_scope.cancel()
|
nursery.cancel_scope.cancel()
|
||||||
|
|
Loading…
Reference in New Issue