我正在调用Python中的一个函数,我知道它可能会停止并迫使我重新启动脚本.
如何调用该函数或我将其包装成什么,以便如果它花费的时间超过5秒,脚本会取消它并执行其他操作?
你会如何提示用户输入一些信息但是在N秒后超时?
谷歌在http://mail.python.org/pipermail/python-list/2006-January/533215.html上指向了一个关于它的邮件线程,但似乎没有用.超时发生的语句,无论是sys.input.readline还是timer.sleep(),我总是得到:
<type'exception.TypeError'>:[raw_]输入最多需要1个参数,得2
以某种方式,除了没有抓住.
我想做一个raw_input('Enter something: .').我希望它睡3秒钟,如果没有输入,则取消提示并运行其余代码.然后代码循环并raw_input再次实现.如果用户输入类似"q"的内容,我也希望它能够破解.
我希望能够做的是使用输入询问用户问题.例如:
print('some scenario')
prompt = input("You have 10 seconds to choose the correct answer...\n")
Run Code Online (Sandbox Code Playgroud)
然后如果时间流逝打印出类似的东西
print('Sorry, times up.')
Run Code Online (Sandbox Code Playgroud)
任何指导我正确方向的帮助将不胜感激.
我正在尝试创建一个循环python函数,它执行任务并提示用户进行响应,如果用户在给定时间内没有响应,则序列将重复.
这是基于这个问题:如何设置raw_input的时间限制
任务由...表示some_function().超时是一个变量,以秒为单位.我有以下代码的两个问题:
无论用户是否提示,raw_input提示在指定的4秒时间后都不会超时.
当输入'q'的raw_input时(没有'',因为我知道键入的任何内容会自动输入为字符串),该函数不会退出循环.
`
import thread
import threading
from time import sleep
def raw_input_with_timeout():
prompt = "Hello is it me you're looking for?"
timeout = 4
astring = None
some_function()
timer = threading.Timer(timeout, thread.interrupt_main)
try:
timer.start()
astring = raw_input(prompt)
except KeyboardInterrupt:
pass
timer.cancel()
if astring.lower() != 'q':
raw_input_with_timeout()
else:
print "goodbye"
Run Code Online (Sandbox Code Playgroud)
`
当用户按下返回键时,我试图让我的脚本触发用户输入.然后主程序将检查txUpdated标志并使用此输入.
我有一个在python中运行的线程,它只是等待用户输入:
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
Run Code Online (Sandbox Code Playgroud)
问题是我不知道如何在没有接收用户输入的情况下结束此线程.即使我的主程序设置了endFlag,线程也不会结束,直到用户再输入一个输入.
有没有人对如何做到这一点有任何建议?
我需要在 Mac 和 Windows 上为 python 3 提供定时输入,如果用户没有时间,它会自动选择其中一个选项。我见过的问题的所有其他答案只有在用户按“Enter”键后才会终止。无论是否按下“Enter”键,我都需要它终止。
例如,在我的国际象棋程序中,如果5秒后没有输入,我需要它自动选择“Q”。另外,如果输入不在 [“Q”,“R”,“B”,“N”] 中,那么我还需要它来选择“Q”。
下面的代码不起作用。它会要求输入然后永远等待;计时器根本不工作。
def countdown():
global timer
timer = 5
for x in range(timer):
timer -= 1
time.sleep(1)
countdownThread = threading.Thread(target=countdown)
countdownThread.start()
while timer > 0:
promotedPiece = input("Promote to Q, R, B or N? > ").upper()
if timer == 0:
break
if timer == 0:
if promotedPiece not in ["Q", "R", "B", "N"]: # If not valid input, choose Queen
promotedPiece = "Q"
print("Queen auto selected")
Run Code Online (Sandbox Code Playgroud) python multithreading timer python-multithreading python-3.x
如果您等待 4 秒钟,它会显示“您的时间用完了”,这很好。但是,为了保持循环继续,您必须按enter键才能继续。
我希望当它在下面打印“你用完了时间”而不是仅仅打字时,它会显示一个输入语句,比如“输入‘攻击’继续前进”,并且循环将从原来的位置继续。
from threading import Timer
import time
monsterhp = int(800)
y = 150
while monsterhp > 0:
timeout = 4
t = Timer(timeout, print, ['You ran out of time.'])
t.start()
print(" ")
prompt = "You have %d seconds Type 'attack' to hit the monster\nType here: " % timeout
answer = input(prompt)
t.cancel()
if answer == "attack":
print("You strike the monster")
time.sleep(1)
monsterhp = monsterhp - y
print("War Lord Health:", monsterhp)
Run Code Online (Sandbox Code Playgroud) 我想要一种方法来生成一个等待用户输入的线程; 如果在10秒内没有输入任何输入,我希望脚本终止生成的线程并继续处理.如果输入文本,我有办法从线程中获取输入但是我无法让超时终止新生成的线程.
在下面的例子中,我是最接近的.我告诉新创建的线程它是一个守护进程,它将在主脚本退出时退出.我对此的问题是该线程将继续等待,直到脚本退出或用户输入了某些内容.
shared_var = ['1']
def run(ref):
ref[0] = raw_input("enter something: ")
print "shared var changed to '%s'" % (ref[0])
thread = threading.Thread(target=run, args=(shared_var,))
thread.daemon = True
thread.start()
time.sleep(10) # simplified timeout
#Need some way to stop thread if no input has been entered
print "shared var = " + shared_var[0]
Run Code Online (Sandbox Code Playgroud)
我知道突然杀死一个线程不是最好的方法(相关链接),但我不知道如何中断等待raw_input的新线程
python ×9
python-3.x ×3
raw-input ×2
timeout ×2
timer ×2
input ×1
loops ×1
python-2.7 ×1
user-input ×1