Skip to content

Commit

Permalink
CommonClient: move to explicit thread instead of thread executor to a…
Browse files Browse the repository at this point in the history
…llow proper task cancelling.
  • Loading branch information
Berserker66 committed Nov 28, 2021
1 parent 5e84900 commit d768379
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
18 changes: 15 additions & 3 deletions CommonClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,14 +540,26 @@ async def process_server_cmd(ctx: CommonContext, args: dict):
ctx.on_package(cmd, args)


def stream_input(stream, queue):
def queuer():
text = stream.readline().strip()
if text:
queue.put_nowait(text)

from threading import Thread
thread = Thread(target=queuer, name=f"Stream handler for {stream.name}", daemon=True)
thread.start()
return thread


async def console_loop(ctx: CommonContext):
import sys
commandprocessor = ctx.command_processor(ctx)
queue = asyncio.Queue()
stream_input(sys.stdin, queue)
while not ctx.exit_event.is_set():
try:
input_text = await asyncio.get_event_loop().run_in_executor(
None, sys.stdin.readline
)
input_text = await queue.get()
input_text = input_text.strip()

if ctx.input_requests > 0:
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
colorama>=0.4.4
websockets>=10.0
websockets>=10.1
PyYAML>=6.0
fuzzywuzzy>=0.18.0
prompt_toolkit>=3.0.22
prompt_toolkit>=3.0.23
appdirs>=1.4.4
jinja2>=3.0.3
schema>=0.7.4

0 comments on commit d768379

Please sign in to comment.