Add `Portal.cancel_called`

zombie_lord_infinite
Tyler Goodlet 2021-10-10 16:43:32 -04:00
parent 66137030d9
commit 8e21bb046e
1 changed files with 21 additions and 5 deletions

View File

@ -84,6 +84,15 @@ class Portal:
] = None ] = None
self._streams: Set[ReceiveMsgStream] = set() self._streams: Set[ReceiveMsgStream] = set()
self.actor = current_actor() self.actor = current_actor()
self._cancel_called: bool = False
@property
def cancel_called(self) -> bool:
'''
Same principle as ``trio.CancelScope.cancel_called``.
'''
return self._cancel_called
async def _submit( async def _submit(
self, self,
@ -197,9 +206,16 @@ class Portal:
# we'll need to .aclose all those channels here # we'll need to .aclose all those channels here
await self._cancel_streams() await self._cancel_streams()
async def cancel_actor(self): async def cancel_actor(self) -> None:
"""Cancel the actor on the other end of this portal. '''
""" Cancel the actor on the other end of this portal.
That means cancelling the "actor runtime" not just any one
task that's running there.
'''
self._cancel_called = True
if not self.channel.connected(): if not self.channel.connected():
log.cancel("This portal is already closed can't cancel") log.cancel("This portal is already closed can't cancel")
return False return False
@ -207,8 +223,8 @@ class Portal:
await self._cancel_streams() await self._cancel_streams()
log.cancel( log.cancel(
f"Sending actor cancel request to {self.channel.uid} on " f"Sending runtime cancel msg to {self.channel.uid} @ "
f"{self.channel}") f"{self.channel.raddr}")
try: try:
# send cancel cmd - might not get response # send cancel cmd - might not get response
# XXX: sure would be nice to make this work with a proper shield # XXX: sure would be nice to make this work with a proper shield