Skip to content

Linux: Handeling multithreading and better terminal settup #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 20 commits into from
Closed

Linux: Handeling multithreading and better terminal settup #82

wants to merge 20 commits into from

Conversation

Cube707
Copy link
Collaborator

@Cube707 Cube707 commented Jul 9, 2022

Originally posted by @qianyun210603 in #79 (comment)

This version indeed solves issue 1 in #64 but unfortunately it doesn't fix issue 2 as per my test on my ovh server (running ubuntu). Below is my test script:

import sys
sys.path.insert(0, r"/root/python-readchar-v4")
from threading import Thread
from queue import Queue
from typing import Callable, Dict
import time
import logging
import readchar

logging.basicConfig(
    format='%(asctime)s - %(levelname)s - %(module)s - %(message)s', level=logging.INFO,
)

running = False


class KeyBoardMonitor(object):

    def __init__(self, callbacks: Dict[str, Callable[[str], None]] = None, quit_symbol: str = 'q'):
        self.callbacks = callbacks if callbacks else {}
        self.quit_symbol = quit_symbol
        self.running = True
        self.queue = Queue()
        self.thread = Thread(name="keyboard monitoring", target=self._keyboard_monitor)

    def register_callback(self, order: str, callback: Callable[[str], None]) -> None:
        self.callbacks[order] = callback

    def unregister_callback(self, order):
        del self.callbacks[order]

    def start(self):
        self.thread.start()

    def stop(self):
        self.running = False

    def _keyboard_monitor(self):
        while self.running:
            ch = str(readchar.readchar())
            if ch in self.callbacks:
                self.callbacks[ch](ch)
            if ch == self.quit_symbol:
                self.running = False

def myprint(ch):
    print(ch, "entered")
    if ch == 'q':
        global running
        running = False


if __name__=="__main__":
    watchdog = KeyBoardMonitor(callbacks={'q': myprint, 'p': myprint})
    watchdog.start()
    running = True
    for _ in range(300):
        if not running:
            break
        time.sleep(1)
        logging.info(f"current_time: {time.time()}")

copy this into file and change the path in sys.path.insert to the "master" branch of local clone of your repo in r"/root/python-readchar-v4", I still see unwanted blanks (should come from the \r generated by setraw and missing linebreaks between logs:
image

In contrast, if using my version in pull request #64, I can get clean log outputs.
image

Would you please take some further look?

Thanks!

Cube707 and others added 20 commits July 4, 2022 13:22
2.7 was not supported or tested for a long time. Since mokypatch
requires at least 3.6, this will be the first supporded version.
The `# -*- coding: utf-8 -*-` headers were useful for Python 2, and
aren't needed for Python 3 where UTF-8 is the default.
LF and CR where swaped. They have the correct values now.
to allow for easier maintanace and proper separation every operation
system gets a seperate set of files. They are exported by the module
depending on `sys.platform`
The new setup uses a seperate folder for every operation system that
tests are provided for. Test for a different operating system than the
one currently running will be ignored.
The testscript now automatically knows of all special keys listed in the
`key.py` submodule. It additionally outputs the hex representaion of all
detected keys
github will now run precommit and pytest on all pushes and PRs
automatically.
fixes #64, as described there, the raw wterminal can produce a lot of
unneeded whitespace. Using the new configuration it seem to behave as
expected.
`kbhit()` requires the terminal to be set up in a special way.
So it is marked as an internal function to discurage use.
@Cube707 Cube707 closed this Jul 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants