如何杀死使用输入的线程?

发布于 2025-01-25 08:52:22 字数 324 浏览 3 评论 0原文

我正在尝试使用插座和线程制作在线游戏。这是一款竞争激烈的游戏游戏,其中最快的游戏能够完成胜利。我希望,当一位球员赢得/跑出生命时,另一个球员被踢出了比赛,并被告知他们赢了/输了。但是,我遇到了一个问题。

1:如果我使用“线程”,则无法关闭线程(由于程序需要读取用户输入,因此我需要使用input()函数,这意味着播放器的线程直到他们的线程才能终止插入角色)。

2:如果我使用“多处理”(具有终止()函数),则无法使用input()(多处理不允许)。

我陷入僵局,我该怎么办?

PD:这是我有史以来的第一个Stackoverflow帖子,请告诉我是否在写帖子时犯了一个错误!

I am trying to make an online game using socket and threading. It is a competitive hangman game, where the fastest one to complete the word wins. I want that, when one of the players wins/runs out of lives, the other player gets kicked out of its game and is told that they have won/lost. However, I have run into a problem.

1: If I use "threading", there is no way to close the thread (since the program needs to read user input, I need to use the input() function, which means that the player's thread can't be terminated until they insert a character).

2: If I use "multiprocessing" (which has a terminate() function), I can't use input() (multiprocessing doesn't allow it).

I am in a stalemate, what should I do?

P.D: This is my first stackoverflow post ever, please tell me if I made a mistake at writing the post!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

她说她爱他 2025-02-01 08:52:22

好的,我明白了,这是一个丑陋而复杂的解决方案,但它起作用。
我创建了一个模拟“输入”命令的程序,而不是输入命令本身(因此它不会停止进程)。
这里是:

import pynput.keyboard

global string
string=""
c=0
global oldprinted
oldprinted=""
def GetInput():
    import pynput
    def on_press(key):
        global oldprinted
        global string
        if key==pynput.keyboard.Key.enter:
            listener.stop()
        else:
            prompt="Introduce your input: "
            try:
                if key==pynput.keyboard.Key.backspace and len(string)>0:
                    string=string[:-1]
                else:
                    string+=str(key.char)
                prompt+=(string)
                print(" "*len(oldprinted),end="\r")
                print(prompt,end="\r")
                oldprinted=prompt
            except:
                pass
    print("Introduce your input: ",end="\r")
    with pynput.keyboard.Listener(suppress=True,on_press=on_press) as listener:
        listener.join()
    return string


data=GetInput()
print("\n")

Ok, I got it, it is an ugly and complicated solution, but it works.
I created a program that emulates the "input" command, without being the input command itself (so it doesn't stop the processes).
Here it is:

import pynput.keyboard

global string
string=""
c=0
global oldprinted
oldprinted=""
def GetInput():
    import pynput
    def on_press(key):
        global oldprinted
        global string
        if key==pynput.keyboard.Key.enter:
            listener.stop()
        else:
            prompt="Introduce your input: "
            try:
                if key==pynput.keyboard.Key.backspace and len(string)>0:
                    string=string[:-1]
                else:
                    string+=str(key.char)
                prompt+=(string)
                print(" "*len(oldprinted),end="\r")
                print(prompt,end="\r")
                oldprinted=prompt
            except:
                pass
    print("Introduce your input: ",end="\r")
    with pynput.keyboard.Listener(suppress=True,on_press=on_press) as listener:
        listener.join()
    return string


data=GetInput()
print("\n")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文