当另一个功能同时 运行ning 时,如何强制作业到 运行?

How to force job to run when another function is running at the same time?

我有一个 while True 语句,它一遍又一遍地 运行 执行相同的功能,还有另外两个功能被安排为每 5 分钟 运行。

但是当作业被触发到运行时,循环中的另一个函数已经运行宁作业被错过了,我收到以下消息:

Run time of job "SendHeroesToWork (trigger: interval[0:03:00], next run at: 2022-01-14 15:33:27 -03)" was missed by 0:00:03.169182

有什么办法可以强制将作业安排在 运行 之前吗?甚至停止当前功能并在合适的时间启动计划。

我的脚本来自 main.py

import asyncio
import tzlocal
from bot import ConnectWallet, LoginMetamask, TreasureHuntGame, NewMap, SkipErrorOnGame, RefreshHeroesPositions, SendHeroesToWork
from controllers import countdownTimer, setup_logger, initializePyAutoGUI, ReadConfigs, DeleteLogFiles
from apscheduler.schedulers.asyncio import AsyncIOScheduler

try:
    streamConfig = ReadConfigs()
    refresh_heroes_time = streamConfig['heroes_options']['refresh_heroes_time']
    refresh_heroes_only = streamConfig['heroes_options']['refresh_heroes_only']
    work_heroes_time = streamConfig['heroes_options']['work_heroes_time']
except FileNotFoundError:
    print('Error: config.yaml file not found, make sure config.yaml are placed in the folder..')
    exit()

async def main():
    # Init message
    print('\nPress Ctrl-C to quit at anytime!\n' )
        
    # Initialize pyautogui library
    await asyncio.create_task(initializePyAutoGUI())

    # Countdown timer before start the bot
    await asyncio.create_task(countdownTimer())

    # Delete old log files
    await asyncio.create_task(DeleteLogFiles())

    # Create a scheduler for certain functions
    scheduler = AsyncIOScheduler(timezone=str(tzlocal.get_localzone()))

    if (refresh_heroes_time*60) > 9:
        scheduler.add_job(RefreshHeroesPositions, 'interval', seconds=(refresh_heroes_time*60)+120, id='1')        

    if (work_heroes_time*60) > 0:
        scheduler.add_job(SendHeroesToWork, 'interval', seconds=(work_heroes_time*60), id='2')

    if len(scheduler.get_jobs()) > 0:
        scheduler.start()  
    
    elif refresh_heroes_only != True:
        while True:
            # Steps of this bot:
            # - Connect Wallet on BomberCypto game            
            await asyncio.create_task(ConnectWallet())
            # - Login Metamask
            await asyncio.create_task(LoginMetamask())
            # - Treasure Hunt game mode
            await asyncio.create_task(TreasureHuntGame())
            # - New map feature
            await asyncio.create_task(NewMap())
            # - Check for errors on game    
            await asyncio.create_task(SkipErrorOnGame())

if __name__ == "__main__":
    try:
        loop = asyncio.get_event_loop()
        loop.create_task(main())
        loop.run_forever()
    except Exception as e:
        #logger = setup_logger()
        #logger.error("Exception: " + str(e))
        print("Exception: " + str(e))
        exit()

实际上这是一个非常简单的解决方案,我没有注意到 apscheduler 中的参数,在 documentation 中解释为:

misfire_grace_time (int) – the time (in seconds) how much this job’s execution is allowed to be late (None means “allow the job to run no matter how late it is”)

所以我将这个参数添加到我的调度程序中,如下所示:

scheduler.add_job(send_heroes_to_work, 'interval', seconds=(work_heroes_time*60), id='2', name='send_heroes_to_work', misfire_grace_time=300)