u/C4tpurr

Complete noob to multithreading, any tips?

I've recently been making a typing game, like the ones I used to have in school to teach kids how to spell. The difference is, in this game you type the binary that makes up the ASCII characters in the word. I've wanted to add a timer that ticks down as you type, but I'm not sure how to get it to run while simultaneously accepting input. After a few searches, I was told to look into multithreading. How can I get a function to continue running, while another part of the script is being executed? Are there any modules you can recommend or other solutions to look into? Thank you for any suggestions!

Edit: Thank you all for the advice! I'm going to try out some of the suggestions. I forgot to mention that I was using the curses library for the project. I discovered that Curses has a method called .nodelay() so I might try that out first.

reddit.com
u/C4tpurr — 4 days ago

Which of these solutions do you prefer?

I'm working on making my own UI library as a practice exercise and came up with these two ways to make a header. I feel like option B is better because creating a new class instance could take up more memory? I'm not really sure and would love advice.

# Option A 
class Header:
    def __init__(self, text):
        self.text = text
        length = 6
        for l in self.text:
            length += 1
        print(f"──┤ {self.text} ├", "─"*(32-length), sep="")

Header("Lorem Ipsum")

# Option B 
def header(text):
    length = 6
    for l in text:
        length += 1
    print(f"──┤ {text} ├", "─"*(32-length), sep="")

header("Lorem Ipsum")
reddit.com
u/C4tpurr — 11 days ago