tidying async messaging

This commit is contained in:
Eric Froemling 2021-09-21 14:12:30 -05:00
parent c8a6c3733d
commit e9cd9a922b
No known key found for this signature in database
GPG Key ID: 89C93F0F8D6D5A98
5 changed files with 44 additions and 27 deletions

View File

@ -2355,6 +2355,7 @@
<w>touchpad</w>
<w>tournamententry</w>
<w>tournamentscores</w>
<w>tpimportex</w>
<w>tpimports</w>
<w>tplayer</w>
<w>tpos</w>

View File

@ -1074,6 +1074,7 @@
<w>touchpad</w>
<w>toucs</w>
<w>toutf</w>
<w>tpimportex</w>
<w>tpimports</w>
<w>tracebacks</w>
<w>tracestr</w>

View File

@ -180,14 +180,9 @@ class _BoundTestSyncMessageReceiver:
self._receiver = receiver
def handle_raw_message(self, message: bytes) -> bytes:
"""Handle a raw incoming synchronous message."""
"""Synchronously handle a raw incoming message."""
return self._receiver.handle_raw_message(self._obj, message)
async def handle_raw_message_async(self, message: bytes) -> bytes:
"""Handle a raw incoming asynchronous message."""
return await self._receiver.handle_raw_message_async(
self._obj, message)
# RCVS_CODE_TEST_END
# RCVA_CODE_TEST_BEGIN
@ -244,12 +239,8 @@ class _BoundTestAsyncMessageReceiver:
self._obj = obj
self._receiver = receiver
def handle_raw_message(self, message: bytes) -> bytes:
"""Handle a raw incoming synchronous message."""
return self._receiver.handle_raw_message(self._obj, message)
async def handle_raw_message_async(self, message: bytes) -> bytes:
"""Handle a raw incoming asynchronous message."""
async def handle_raw_message(self, message: bytes) -> bytes:
"""Asynchronously handle a raw incoming message."""
return await self._receiver.handle_raw_message_async(
self._obj, message)
@ -419,7 +410,7 @@ def test_full_pipeline() -> None:
# (we can do sync or async receivers)
if isinstance(self._target, TestClassRSync):
return self._target.receiver.handle_raw_message(data)
return await self._target.receiver.handle_raw_message_async(data)
return await self._target.receiver.handle_raw_message(data)
class TestClassRSync:
"""Test class incorporating synchronous receive functionality."""

View File

@ -279,8 +279,10 @@ class MessageProtocol:
if part == 'sender':
importlines1 = 'from efro.message import MessageSender'
tpimportex = ''
else:
importlines1 = 'from efro.message import MessageReceiver'
tpimportex = ', Awaitable'
out = ('# Released under the MIT License. See LICENSE for details.\n'
f'#\n'
@ -293,7 +295,8 @@ class MessageProtocol:
f'{importlines1}\n'
f'\n'
f'if TYPE_CHECKING:\n'
f' from typing import Union, Any, Optional, Callable\n'
f' from typing import Union, Any, Optional, Callable'
f'{tpimportex}\n'
f'{importlines2}'
f'\n'
f'\n')
@ -480,19 +483,23 @@ class MessageProtocol:
f' ) -> None:\n'
f' assert obj is not None\n'
f' self._obj = obj\n'
f' self._receiver = receiver\n'
f'\n'
f' def handle_raw_message(self, message: bytes) -> bytes:\n'
f' """Handle a raw incoming synchronous message."""\n'
f' return self._receiver.handle_raw_message'
f'(self._obj, message)\n'
f'\n'
f' async def handle_raw_message_async(self, message: bytes)'
f' -> bytes:\n'
f' """Handle a raw incoming asynchronous message."""\n'
f' return await'
f' self._receiver.handle_raw_message_async(\n'
f' self._obj, message)\n')
f' self._receiver = receiver\n')
if is_async:
out += (
'\n'
' async def handle_raw_message(self, message: bytes)'
' -> bytes:\n'
' """Asynchronously handle a raw incoming message."""\n'
' return await'
' self._receiver.handle_raw_message_async(\n'
' self._obj, message)\n')
else:
out += (
'\n'
' def handle_raw_message(self, message: bytes) -> bytes:\n'
' """Synchronously handle a raw incoming message."""\n'
' return self._receiver.handle_raw_message'
'(self._obj, message)\n')
return out
@ -744,6 +751,7 @@ class MessageReceiver:
def handle_raw_message(self, bound_obj: Any, msg: bytes) -> bytes:
"""Decode, handle, and return an encoded response for a message."""
assert not self.is_async, "can't call sync handler on async receiver"
try:
msg_decoded, msgtype = self._decode_incoming_message(msg)
handler = self._handlers.get(msgtype)
@ -761,6 +769,7 @@ class MessageReceiver:
The return value is the raw response to the message.
"""
assert self.is_async, "can't call async handler on sync receiver"
try:
msg_decoded, msgtype = self._decode_incoming_message(msg)
handler = self._handlers.get(msgtype)

View File

@ -235,6 +235,21 @@ def check_clean_safety() -> None:
sys.exit(255)
def gen_empty_py_init() -> None:
"""Generate an empty __init__.py for a package dir.
Used as part of meta builds.
"""
from efro.terminal import Clr
if len(sys.argv) != 3:
raise Exception('Expected a single path arg.')
outpath = Path(sys.argv[2])
outpath.parent.mkdir(parents=True, exist_ok=True)
print(f'Meta-building {Clr.BLD}{outpath}{Clr.RST}')
with open(outpath, 'w', encoding='utf-8') as outfile:
outfile.write('# This file is autogenerated; do not hand-edit.\n')
def formatcode() -> None:
"""Format all of our C/C++/etc. code."""
import efrotools.code