FEARLESS CONCURRENCY

This commit is contained in:
Anthony Sottile 2020-05-16 13:56:47 -07:00
parent a31601483a
commit 826f42fa65
1 changed files with 32 additions and 18 deletions

48
bot.py
View File

@ -595,6 +595,34 @@ def _shutdown(
closing_task.add_done_callback(cancel_tasks) closing_task.add_done_callback(cancel_tasks)
async def handle_response(
config: Config,
match: Match[str],
handler: Callable[[Match[str]], Response],
writer: asyncio.StreamWriter,
*,
quiet: bool,
) -> None:
try:
res = await handler(match)(config)
except Exception as e:
traceback.print_exc()
res = PRIVMSG.format(
channel=config.channel,
msg=f'*** unhandled {type(e).__name__} -- see logs',
)
if res is not None:
send_match = SEND_MSG_RE.match(res)
if send_match:
color = '\033[1m\033[3m\033[38;5;21m'
print(
f'{dt_str()}'
f'<{color}{config.username}\033[m> '
f'{send_match[1]}',
)
await send(writer, res, quiet=quiet)
async def amain(config: Config, *, quiet: bool) -> None: async def amain(config: Config, *, quiet: bool) -> None:
reader, writer = await asyncio.open_connection(HOST, PORT, ssl=True) reader, writer = await asyncio.open_connection(HOST, PORT, ssl=True)
@ -650,24 +678,10 @@ async def amain(config: Config, *, quiet: bool) -> None:
for pattern, handler in HANDLERS: for pattern, handler in HANDLERS:
match = pattern.match(msg) match = pattern.match(msg)
if match: if match:
try: coro = handle_response(
res = await handler(match)(config) config, match, handler, writer, quiet=quiet,
except Exception as e:
traceback.print_exc()
res = PRIVMSG.format(
channel=config.channel,
msg=f'*** unhandled {type(e).__name__} -- see logs',
) )
if res is not None: loop.create_task(coro)
send_match = SEND_MSG_RE.match(res)
if send_match:
color = '\033[1m\033[3m\033[38;5;21m'
print(
f'{dt_str()}'
f'<{color}{config.username}\033[m> '
f'{send_match[1]}',
)
await send(writer, res, quiet=quiet)
break break
else: else:
if not quiet: if not quiet: