Python:停止等待用户输入的线程 [英] Python: Stop thread that is waiting for user input

查看:88
本文介绍了Python:停止等待用户输入的线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我的脚本在用户按下返回键时触发用户输入.然后主程序将检查 txUpdated 标志并使用此输入.

I'm trying to have my script trigger a user input when the user pressed the return key. The main program will then check the txUpdated flag and use this input.

我有一个在 python 中运行的线程,它只是等待用户输入:

I have a thread running in python that simply waits for user input:

class InputThread(threading.Thread):
    def __init__(self, threadID, name):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
    def run(self):
        global screenLock
        global txUpdated
        global txMessage
        global endFlag
        lock = threading.Lock()

        print "Starting " + self.name
        while not endFlag:
            txMessage = raw_input()
            if (txMessage == ""):
                screenLock = 1
                txMessage = raw_input("Enter Tx String: ")
                screenLock = 0

                with lock:
                    txUpdated = 1

        print "Exiting " + self.name

问题是我不知道如何在没有收到用户输入的情况下随时结束此线程.即使我的主程序设置了 endFlag,线程也不会结束,直到用户再输入一个输入.

The problem is I don't know how to end this thread at any point without receiving a user input. Even if my main program sets the endFlag the thread wont end until the user inputs one more input.

有人对如何实现这一点有任何建议吗?

Does anyone have any suggestions on how to accomplish this?

推荐答案

这是一个仅适用于 Windows 的解决方案,基于 这个答案 作者:Alex Martelli:

Here is a Windows-only solution, based on this answer by Alex Martelli:

import msvcrt
import time
import threading

endFlag = False

class InputThread(threading.Thread):
    def __init__(self, threadID, name):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name

    def run(self):
        global screenLock
        global txUpdated
        global txMessage
        lock = threading.Lock()
        print "Starting " + self.name
        while not endFlag:
            txMessage = self.raw_input_with_cancel()  # This can be cancelled by setting endFlag
            if (txMessage == ""):
                screenLock = 1
                txMessage = raw_input("Enter Tx String: ")
                screenLock = 0

                with lock:
                    txUpdated = 1

        print "Exiting " + self.name

    def raw_input_with_cancel(self, prompt=None):
        if prompt:
            print prompt,
        result = []
        while True:
            if msvcrt.kbhit():
                result.append(msvcrt.getche())
                if result[-1] in ['\r', '\n']:
                    print
                    return ''.join(result).rstrip()
            if endFlag:
                return None
            time.sleep(0.1)  # just to yield to other processes/threads

endFlag设置为True时,线程将退出.

When endFlag is set to True, the thread will exit.

这篇关于Python:停止等待用户输入的线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆