我正在调用Python中的一个函数,我知道它可能会停止并迫使我重新启动脚本.
如何调用该函数或我将其包装成什么,以便如果它花费的时间超过5秒,脚本会取消它并执行其他操作?
我想做一个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中,有没有办法在等待用户输入时计算时间,以便在30秒后raw_input()自动跳过该函数?
我正在尝试创建一个循环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)
`
在 Python 2 中有一个函数thread.interrupt_main(),KeyboardInterrupt当从子线程调用时,它会在主线程中引发异常。
这也可以_thread.interrupt_main()在 Python 3 中使用,但它是一个低级的“支持模块”,主要用于其他标准模块。
在 Python 3 中这样做的现代方法是什么,大概是通过threading模块,如果有的话?
python multithreading keyboardinterrupt python-multithreading python-3.x
有人可以提供一个示例代码,这些代码示例使用asynio以非阻塞方式监听按键并在每次单击时将其放入控制台吗?
这不是一些图形工具包的问题
我正在尝试将函数的结果写入stdin.
这是代码:
def testy():
return 'Testy !'
import sys
sys.stdin.write(testy())
Run Code Online (Sandbox Code Playgroud)
而我得到的错误是:
Traceback (most recent call last):
File "stdin_test2.py", line 7, in <module>
sys.stdin.write(testy())
io.UnsupportedOperation: not writable
Run Code Online (Sandbox Code Playgroud)
我不完全确定,这是正确的做事方式吗?
首先,我使用的是 python 2.7.5 和 Windows x64,我的应用程序针对这些参数。
在经过一定时间后,我需要一种取消 raw_input 的方法。目前我的主线程启动了两个子线程,一个是计时器(threading.Timer),另一个触发 raw_input。它们都向主线程监视的 Queue.queue 返回一个值。然后它对发送到队列的内容进行操作。
# snip...
q = Queue.queue()
# spawn user thread
user = threading.Thread(target=user_input, args=[q])
# spawn timer thread (20 minutes)
timer = threading.Timer(1200, q.put, ['y'])
# wait until we get a response from either
while q.empty():
time.sleep(1)
timer.cancel()
# stop the user input thread here if it's still going
# process the queue value
i = q.get()
if i in 'yY':
# do yes stuff here
elif i in …Run Code Online (Sandbox Code Playgroud) 我正在使用Python 3,我想编写一个程序,在一定时间内要求多个用户输入.这是我的尝试:
from threading import Timer
##
def timeup():
global your_time
your_time = False
return your_time
##
timeout = 5
your_Time = True
t = Timer(timeout, timeup)
t.start()
##
while your_time == True:
input()
t.cancel()
print('Stop typing!')
Run Code Online (Sandbox Code Playgroud)
问题是,即使时间到了,代码仍然等待输入.我希望循环在时间用完时完全停止.我该怎么做呢?谢谢!
问题是,RuntimeError: Event loop is closed即使我使用return_when=asyncio.FIRST_COMPLETEDinside ,我也会不断出错await asyncio.wait()。
我的代码:
async def task_manager():
tasks = [grab_proxy() for _ in range(10)]
finished, unfinished = await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED)
for x in finished:
result = x.result()
if result:
return result
def get_proxy_loop():
loop = asyncio.new_event_loop()
proxy = loop.run_until_complete(task_manager())
loop.close()
return proxy
if __name__ == '__main__':
p = get_proxy_loop()
print(type(p))
print(p)
Run Code Online (Sandbox Code Playgroud)
预期行为:
return_when=asyncio.FIRST_COMPLETED 当第一个结果返回“幕后”时,应该终止所有剩余的任务。
但实际上在返回第一个结果后仍然存在未完成的任务。在我关闭循环get_proxy_loop()并访问__main__这些剩余任务中的结果raise 之后RuntimeError: Event loop is closed。
控制台输出:
<class 'str'>
78.32.35.21:55075 …Run Code Online (Sandbox Code Playgroud) 我想使用秒表来计时我的代码,但我需要帮助。这是我的代码:
import time
timer = 0
alreadyout = 'no'
eligable = 'no'
entered = 'no'
if alreadyout == 'no' and eligable == 'no':
answer = str(input("type to 100 letters as fast as you can"))
if len(answer) == 100:
eliable = 'yes'
entered = 'yes'
alreadyout = 'yes'
while entered == 'no':
time.sleep(1)
timer = timer + 1
print(timer)
Run Code Online (Sandbox Code Playgroud) 我试图弄清楚如何创建一个可以采用以下参数的小脚本:
最后一个是在程序停止接受字符并开始处理输入之前我可以输入的字符数.我见过有些人使用Python的select.select 方法,但这并没有考虑到第3项.我倾向于诅咒,虽然我不知道它是否支持超时让我想到线程.任何见解都会很棒!这将在Linux上使用Python 2.6运行.