首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在某个特定时间运行python程序的一部分

如何在某个特定时间运行python程序的一部分
EN

Stack Overflow用户
提问于 2019-07-19 18:50:48
回答 1查看 302关注 0票数 0

我想在早上7时在python程序中运行我的函数。几乎就像一个when函数when time == time:

我不想使用cron,因为我希望这是内部函数,而不是执行整个脚本

下面是我创建的python脚本:

代码语言:javascript
复制
#sprinkler.py file for sprinkler system
#this is the main file for the sprinklerOS

#functions to import for our test_run.py file
import RPi.GPIO as GPIO
import time
import argparse
import sys
import datetime
#how many zones the user/YOU have set up
zone_count = 10
#gpio pins on the raspberry pi for sprinkler system
pin = [12,7,16,11,18,13,22,15,32,29]

#set mode to board meaning the pin number on pi (1-40) instead of gpio number
GPIO.setmode(GPIO.BOARD)

GPIO.setwarnings(False)


#setting up all of the lights or relays as outputs(in a while loop)
for a in range(10):
        GPIO.setup(pin[a],GPIO.OUT)
        GPIO.output(pin[a], GPIO.LOW)



def run_sprinklers():
        for b in range(zone_count):
                run_time = 5
                GPIO.output(pin[b], GPIO.HIGH)
                time.sleep(run_time)
                GPIO.output(pin[b], GPIO.LOW)
                time.sleep(1)

早上7点的时候我想要run_sprinklers(),谢谢你的进阶

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-19 19:00:51

正如其他人在评论中提到的,cron可能是最好的解决方案。但是,如果这是一个持久的过程,并且您不能依赖像cron这样的工具--调度模块https://github.com/dbader/schedule --您可能会感兴趣。

代码语言:javascript
复制
import RPi.GPIO as GPIO
import time
import argparse
import sys
import datetime
import schedule

#how many zones the user/YOU have set up
zone_count = 10
#gpio pins on the raspberry pi for sprinkler system
pin = [12,7,16,11,18,13,22,15,32,29]

#set mode to board meaning the pin number on pi (1-40) instead of gpio number
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)


#setting up all of the lights or relays as outputs(in a while loop)
for a in range(10):
        GPIO.setup(pin[a],GPIO.OUT)
        GPIO.output(pin[a], GPIO.LOW)


def run_sprinklers():
        for b in range(zone_count):
                run_time = 5
                GPIO.output(pin[b], GPIO.HIGH)
                time.sleep(run_time)
                GPIO.output(pin[b], GPIO.LOW)
                time.sleep(1)


schedule.every().day.at("7:00").do(run_sprinklers)


while True:
    schedule.run_pending()
    time.sleep(1)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57118176

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档