しばらくこれに苦労しています。
このスレッドに基づく:グローバル変数を作成した関数以外の関数で使用する
特定の時間にタスクをスケジュールすることで、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')
scheduler.add_job(night_limits, 'cron', hour=19, minute=32,misfire_grace_time=3600,timezone='GB')
scheduler.start()
scheduler.print_jobs()
executor = ProcessPoolExecutor(1)
loop = asyncio.get_event_loop()
baa = asyncio.async(loop.run_in_executor(executor, thread_2))
try:
loop.run_forever()
except (KeyboardInterrupt, Exception):
loop.stop()
scheduler.shutdown()
結果:
19:31:54 - Variable: 60
19:31:56 - Variable: 60
19:31:58 - Variable: 60
Night Variable: 65
19:32:00 - Variable: 60
19:32:02 - Variable: 60
私は何かが欠けていますが、何が見えません!
考え?
ありがとう!!!