【问题标题】:make python script run for a minute让python脚本运行一分钟
【发布时间】:2018-07-07 09:52:18
【问题描述】:

我正在开发一个小项目,其中的脚本用于监控用户的键盘输入,我只希望脚本运行 1 分钟。在那一分钟过去后,我想要输入的最终打印语句并终止脚本。 time.sleep 函数在这里不是一个可行的选择,因为我想更新变量并接收每个动作的输出,而使用 sleep 只会延迟每个输入。

from pynput import keyboard

word_counter = 0


def on_press(key):
    global word_counter
    try:
        print('alphabet key {} pressed'.format(key.char))
    except AttributeError:
        if key == keyboard.Key.space:
            word_counter += 1
            print(word_counter)
        elif key == keyboard.Key.esc:
            return False
        print('special key {} pressed'.format(key))


with keyboard.Listener(on_press=on_press) as listener:
    listener.join()

# After a minute, this will be the final output and the program will terminate
print('You typed a total of {} words in a minute'.format(word_counter))

【问题讨论】:

  • 那么,哪个部分应该运行 1 分钟?我不明白这段代码是什么..
  • 整个脚本应该运行 1 分钟。该代码基本上只是监视我的输入。一段时间后,我想终止整个程序。
  • 您可能会在这里找到答案,这可能会帮助您解决问题:stackoverflow.com/questions/2831775/…

标签: python time scripting


【解决方案1】:

这就是答案:

from pynput import keyboard
import threading, time

word_counter = 0

def background():
    def on_press(key):
        global word_counter
        try:
            print('alphabet key {} pressed'.format(key.char))
        except AttributeError:
            if key == keyboard.Key.space:
                word_counter += 1
                print(word_counter)
            elif key == keyboard.Key.esc:
                return False
            print('special key {} pressed'.format(key))

    with keyboard.Listener(on_press=on_press) as listener:
        listener.join()

def wait():
    time.sleep(60)

background = threading.Thread(name = 'background', target = background)
background.start()
wait()

# After a minute, this will be the final output and the program will terminate
print('You typed a total of {} words in a minute'.format(word_counter))

【讨论】:

  • 这成功了!谢谢,我知道它与线程有关,但只是不知道如何在我的项目中实现它。
  • 欢迎来到 Stack Overflow!
  • 为什么它对我不起作用? Mac,Python 2.7。识别特殊字符,但即使我在给定时间内输入了几十个单词,程序最终也会说 0 个单词。并且它不会在 60 秒后终止。如果我按下任何特殊键(例如 cmd、ctrl 等),则继续识别特殊键
  • @bisherbas 好吧,我认为 python 版本很重要。我用 Python 3.5 运行了这个程序。用升级版试试那个程序怎么样?如果它再次不起作用,请随时告诉我
  • @HoseongJeon 不幸的是,Python 3.7 的行为相同
【解决方案2】:
from pynput import keyboard
import time

word_counter = 0


def on_press(key):
    global word_counter
    try:
        print('alphabet key {} pressed'.format(key.char))
    except AttributeError:
        if key == keyboard.Key.space:
            word_counter += 1
            print(word_counter)
        elif key == keyboard.Key.esc:
            return False
        print('special key {} pressed'.format(key))
i=int(time.time())+60
while(time.time()<=i):
     with keyboard.Listener(on_press=on_press) as listener:
          listener.join()

# After a minute, this will be the final output and the program will terminate
print('You typed a total of {} words in a minute'.format(word_counter))

我相信这段代码会运行 1 分钟。

例如:

import time 

i=int(time.time())+60
print("ddnd")
while(int(time.time())<=i):
    print("dlksnd")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-04
    • 2018-11-10
    • 2017-08-16
    • 2023-02-19
    • 2016-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多