如何每x秒强制执行python脚本 [英] How force a python script every x seconds

查看:156
本文介绍了如何每x秒强制执行python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试过schedule库,但是我的代码有时会停顿,这也意味着计划代码会停顿.这是代码:

I have tried the schedule library however my code stalls sometimes which also means the schedule code stalls. Here's the code:

import schedule
import time
import app

schedule.every(3).seconds.do(app.run)
while 1:
    try:
        schedule.run_pending()
    except Exception as e:
        print e
        pass

app.run是脚本,它使用队列和线程来请求交换数据.其中一个交换随机抛出一个错误,并且由于线程化(我认为),代码进入了混乱状态.我似乎无法修复该错误,但对该问题进行的肮脏修复将是每x次强制运行该脚本(在我的情况下,我需要10秒)有什么想法吗?

app.run is the script, it using Queues and Threads to request exchange data. One of the exchanges randomly throws an error and because the of threading (I think) the code goes into a state of limbo. I can't seem to fix the bug, but a dirty fix to the problem would be force run the script every x time (in my case I want 10 seconds) Any ideas ?

推荐答案

以下代码将起作用

import sched
import app
import time
seconds = int(input())
sc = sched.scheduler(time.time, time.sleep)
sc.enter(seconds, 1, app.run, (sc,))
sc.run()

您可以获得更多信息这里

这篇关于如何每x秒强制执行python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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