在用户指定的时间内运行 Python 脚本 [英] Running a Python script for a user-specified amount of time

查看:39
本文介绍了在用户指定的时间内运行 Python 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天刚刚开始学习 Python.我一直在阅读 Python 字节.现在我有一个涉及时间的 Python 项目.我在 Byte of Python 中找不到任何与时间相关的内容,所以我会问你:

I've just started learning Python today. I've been reading a Byte of Python. Right now I have a project for Python that involves time. I can't find anything relating to time in Byte of Python, so I'll ask you:

如何在用户指定的时间内运行块然后中断?

How can I run a block for a user specified amount of time and then break?

例如(在一些伪代码中):

For example (in some pseudo-code):

time = int(raw_input('Enter the amount of seconds you want to run this: '))
while there is still time left:
    #run this block

甚至更好:

import sys
time = sys.argv[1]
while there is still time left:
    #run this block

推荐答案

我建议生成另一个 线程,使其成为守护线程,然后休眠,直到您希望任务终止.例如:

I recommend spawning another thread, making it a daemon thread, then sleeping until you want the task to die. For example:

from time import sleep
from threading import Thread

def some_task():
    while True:
        pass

t = Thread(target=some_task)  # run the some_task function in another
                              # thread
t.daemon = True               # Python will exit when the main thread
                              # exits, even if this thread is still
                              # running
t.start()

snooziness = int(raw_input('Enter the amount of seconds you want to run this: '))
sleep(snooziness)

# Since this is the end of the script, Python will now exit.  If we
# still had any other non-daemon threads running, we wouldn't exit.
# However, since our task is a daemon thread, Python will exit even if
# it's still going.

当所有非守护线程都退出时,Python 解释器将关闭.因此,当您的主线程退出时,如果唯一运行的其他线程是您在单独的守护线程中运行的任务,那么 Python 将退出.如果您希望能够直接退出而不用担心手动导致它退出并等待它停止,那么这是一种在后台运行某些东西的便捷方式.

The Python interpreter will shut down when all non-daemon threads have exited. So when your main thread exits, if the only other thread running is your task that you are running in a separate daemon thread, then Python will just exit. This is a convenient way of running something in the background if you want to be able to just quit without worrying about manually causing it to quit and waiting for it to stop.

所以换句话说,这种方法比在 for 循环中使用 sleep 的优势在于,在这种情况下,您必须以将任务分解为离散块的方式对任务进行编码,并且然后每隔一段时间检查一下你的时间是否到了.这对您的目的来说可能没问题,但它可能会出现问题,例如每个块是否需要大量时间,从而导致您的程序运行时间比用户输入的时间长得多,等等.这对您来说是否是一个问题取决于您正在编写的任务,但我想我会提到这种方法,以防它对您更好.

So in other words, the advantage which this approach has over using sleep in a for loop is in that case you have to code your task in such a way that it's broken up into discrete chunks and then check every so often whether your time is up. Which might be fine for your purposes, but it can have problems, such as if each chunk takes a significant amount of time, thus causing your program to run for significantly longer than what the user entered, etc. Whether this is a problem for you depends on the task you are writing, but I figured I would mention this approach in case it would be better for you.

这篇关于在用户指定的时间内运行 Python 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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