小编Lik*_*nge的帖子

Python 3 - 带有 AsyncIO/APScheduler 的全局变量

已经为此苦苦挣扎了一段时间。

基于此线程:在函数中使用全局变量而不是创建它们的函数

我应该能够通过在特定时间安排任务来更新 thread_2 使用的变量。

编码:

import asyncio
from concurrent.futures import ProcessPoolExecutor
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from datetime import datetime
import time


def day_limits():

        global variable
        variable = 90
        print ('Day Variable: ',variable)

def night_limits():

        global variable
        variable = 65
        print ('Night Variable: ',variable)


def thread_2():


    while True:

        c_hour = int(datetime.now().strftime("%H"))
        c_min = int(datetime.now().strftime("%M"))
        c_sec = int(datetime.now().strftime("%S"))

        print ('%02d:%02d:%02d - Variable: %d ' % (c_hour,c_min,c_sec,variable))

        time.sleep(2)


if __name__ == "__main__":

    variable = 60

    scheduler = AsyncIOScheduler()
    scheduler.add_job(day_limits, 'cron', hour=7,misfire_grace_time=3600,timezone='GB') …
Run Code Online (Sandbox Code Playgroud)

python global global-variables apscheduler python-asyncio

5
推荐指数
1
解决办法
1万
查看次数