Skip to content

Commit

Permalink
Change use asyncio task
Browse files Browse the repository at this point in the history
  • Loading branch information
blavka committed Apr 26, 2022
1 parent 7e8d0cb commit bd5c330
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/hardwario/chester/console.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import threading
import os
import asyncio
import logging
from functools import partial
from datetime import datetime
from loguru import logger
from prompt_toolkit.eventloop.utils import get_event_loop
from prompt_toolkit.application import Application
from prompt_toolkit.buffer import Buffer
from prompt_toolkit.key_binding import KeyBindings
Expand Down Expand Up @@ -143,7 +145,7 @@ def run(self, prog: NRFJProg, console_file):
if 'Terminal' not in channels:
raise Exception('Not found RTT Terminal channel')

def task_rtt_read(channel, buffer):
async def task_rtt_read(channel, buffer):
while prog.rtt_is_running:
with logger.catch(message='task_rtt_read', reraise=True):
try:
Expand All @@ -160,15 +162,19 @@ def task_rtt_read(channel, buffer):

line = line.replace('\r', '')
buffer.set_document(Document(buffer.text + line, None), True)
await asyncio.sleep(0.0001)

console_file.write(f'{ "*" * 80 }\n')

t1 = threading.Thread(target=task_rtt_read, args=('Logger', self.logger_buffer))
t2 = threading.Thread(target=task_rtt_read, args=('Terminal', self.shell_buffer))
t1.daemon = True
t2.daemon = True
t1.start()
t2.start()
async def task_rtt_read_logger():
await task_rtt_read('Logger', self.logger_buffer)

async def task_rtt_read_terminal():
await task_rtt_read('Terminal', self.shell_buffer)

loop = get_event_loop()
loop.create_task(task_rtt_read_logger())
loop.create_task(task_rtt_read_terminal())

def accept(buff):
line = f'{buff.text}\n'.replace('\r', '')
Expand All @@ -183,4 +189,5 @@ def accept(buff):
self.input_field.accept_handler = accept

self.app.run()

prog.rtt_stop()

0 comments on commit bd5c330

Please sign in to comment.