亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

10 秒后重新運行 python 腳本

10 秒后重新運行 python 腳本

慕斯709654 2023-12-20 16:17:16
我試圖每 10 秒重新運行一次我的 python 腳本。我已經嘗試了 time.sleep 函數的各種方法,但似乎沒有任何效果。我嘗試過 time.sleep 的不同變體: def job()   # scriptif __name__ == '__main__':    while True:        job()        time.sleep(10)但沒有找到正確的地點/方式來實施它。我不想重新運行函數,而是完整的代碼。這是我嘗試重新運行的腳本:import...def main():    # parse the command line arguments    args = ConfigArgumentParser(description=__doc__).parse_args()    if _debug: _log.debug("initialization")    if _debug: _log.debug("    - args: %r", args)    # make a device object    this_device = LocalDeviceObject(        objectName=args.ini.objectname,        objectIdentifier=('device', int(args.ini.objectidentifier)),        maxApduLengthAccepted=int(args.ini.maxapdulengthaccepted),        segmentationSupported=args.ini.segmentationsupported,        vendorIdentifier=int(args.ini.vendoridentifier),    )    # make a sample application    this_application = BIPSimpleApplication(this_device, args.ini.address)    # make some random input objects    for i in range(1, RANDOM_OBJECT_COUNT + 1):        ravo = RandomAnalogValueObject(            objectIdentifier=('analogValue', i),            objectName='Temp%d' % (i,),        )        this_application.add_object(ravo)    # make sure they are all there    _log.debug("    - object list: %r", this_device.objectList)    _log.debug("running")    run()    _log.debug("fini")if __name__ == "__main__":    main()
查看完整描述

3 回答

?
米脂

TA貢獻1836條經驗 獲得超3個贊

如果您需要運行整個過程(甚至是引導代碼),您可以使用 shell 腳本來完成。


但這不會刷新環境變量。


#!/usr/bin/env bash


while true; do?

? sleep 10

? python script.py

done

如果您需要刷新環境變量(由于 RANDOM_OBJECTS,您似乎需要它),請添加eval "$(exec /usr/bin/env -i "${SHELL}" -l -c "export")"到其中。


查看完整回答
反對 回復 2023-12-20
?
RISEBY

TA貢獻1856條經驗 獲得超5個贊

嘗試這個:

if __name__ == "__main__":
    time.sleep(10)
    main()


查看完整回答
反對 回復 2023-12-20
?
慕哥6287543

TA貢獻1831條經驗 獲得超10個贊

你能做的就是在 10 秒后重新啟動整個腳本。


應用于您的代碼,它看起來像這樣:


import os

import time

import json

import sys


from bacpypes.debugging import bacpypes_debugging, ModuleLogger

from bacpypes.consolelogging import ConfigArgumentParser


from bacpypes.core import run


from bacpypes.primitivedata import Real

from bacpypes.object import AnalogValueObject, Property, register_object_type

from bacpypes.errors import ExecutionError


from bacpypes.app import BIPSimpleApplication

from bacpypes.local.device import LocalDeviceObject



# Read Json

with open('ObjectList.json') as json_file:

    data = json.load(json_file)



    def get_present_value(no):

        for a in data['AnalogValues']:

            if a['ObjectIdentifier'] == int(no):

                return a['PresentValue']

        return None


ai_1 = (get_present_value(1))


print(ai_1)


# some debugging

_debug = 0

_log = ModuleLogger(globals())


# settings

RANDOM_OBJECT_COUNT = int(os.getenv('RANDOM_OBJECT_COUNT', 1))



#

#   RandomValueProperty

#


class RandomValueProperty(Property):


    def __init__(self, identifier):

        if _debug: RandomValueProperty._debug("__init__ %r", identifier)

        Property.__init__(self, identifier, Real, default=0.0, optional=True, mutable=False)


    def ReadProperty(self, obj, arrayIndex=None):

        if _debug: RandomValueProperty._debug("ReadProperty %r arrayIndex=%r", obj, arrayIndex)


        # access an array

        if arrayIndex is not None:

            raise ExecutionError(errorClass='property', errorCode='propertyIsNotAnArray')


        # return a random value


        value = ai_1


        if _debug: RandomValueProperty._debug("    - value: %r", value)


        return value



    def WriteProperty(self, obj, value, arrayIndex=None, priority=None, direct=False):

        if _debug: RandomValueProperty._debug("WriteProperty %r %r arrayIndex=%r priority=%r direct=%r", obj, value,

                                              arrayIndex, priority, direct)

        raise ExecutionError(errorClass='property', errorCode='writeAccessDenied')



bacpypes_debugging(RandomValueProperty)


#

#   Random Value Object Type

#


class RandomAnalogValueObject(AnalogValueObject):

    properties = [

        RandomValueProperty('presentValue'),

    ]


    def __init__(self, **kwargs):

        if _debug: RandomAnalogValueObject._debug("__init__ %r", kwargs)

        AnalogValueObject.__init__(self, **kwargs)



bacpypes_debugging(RandomAnalogValueObject)

register_object_type(RandomAnalogValueObject)



#

#   __main__

#


def main():

    # parse the command line arguments

    args = ConfigArgumentParser(description=__doc__).parse_args()


    if _debug: _log.debug("initialization")

    if _debug: _log.debug("    - args: %r", args)


    # make a device object

    this_device = LocalDeviceObject(

        objectName=args.ini.objectname,

        objectIdentifier=('device', int(args.ini.objectidentifier)),

        maxApduLengthAccepted=int(args.ini.maxapdulengthaccepted),

        segmentationSupported=args.ini.segmentationsupported,

        vendorIdentifier=int(args.ini.vendoridentifier),


    )


    # make a sample application

    this_application = BIPSimpleApplication(this_device, args.ini.address)


    # make some random input objects

    for i in range(1, RANDOM_OBJECT_COUNT + 1):

        ravo = RandomAnalogValueObject(

            objectIdentifier=('analogValue', i),

            objectName='Temp%d' % (i,),


        )


        this_application.add_object(ravo)


    # make sure they are all there


    _log.debug("    - object list: %r", this_device.objectList)


    _log.debug("running")


    run()


    _log.debug("fini")



if __name__ == "__main__":

    main()

    time.sleep(10)

    os.execl(sys.executable, sys.executable, * sys.argv)  # Your script restarts after 10s with this



查看完整回答
反對 回復 2023-12-20
  • 3 回答
  • 0 關注
  • 257 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號