我正在尝试启用/禁用类变量。当我调用禁用函数时,我可以看到函数的变化。但在函数之外,它仍然读取为启用。
警报.py
Class Alarm:
def __init__(self):
self.alarm_enabled = None
def alarm_toggle(self, status):
self.alarm_enabled = status
电报机器人.py
#!/usr/bin/env python
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
import logging
import os
import datetime
from settings import Settings
from alarm import Alarm
import time
alarmpi = Alarm()
def alarmenable(bot, update):
if (alarmpi.alarm_enabled == True):
update.message.reply_text("Alarm is already enabled")
else:
update.message.reply_text("Alarm has been enabled")
alarmpi.alarm_toggle(True)
print('From subfile: ' + str(alarmpi.alarm_enabled))
def alarmdisable(bot, update):
if (alarmpi.alarm_enabled == False):
update.message.reply_text("Alarm is already disabled")
else:
alarmpi.alarm_toggle(False)
update.message.reply_text("Alarm has been disabled")
print('From subfile: ' + str(alarmpi.alarm_enabled))
def main():
# Start the Bot
updater.start_polling()
print('Bot Active')
# Run the bot until the you presses Ctrl-C or the process receives SIGINT,
# SIGTERM or SIGABRT. This should be used most of the time, since
# start_polling() is non-blocking and will stop the bot gracefully.
updater.idle()
def timer():
while True:
print('From timer: ' + str(alarmpi.alarm_enabled))
time.sleep(5)
主文件
import datetime
import time
from multiprocessing import Process
from settings import Settings
import telegram_bot
from alarm import Alarm
def run_bot():
telegram_bot.main()
def alarm_timer():
telegram_bot.timer()
if __name__ == '__main__':
bot = Process(target = run_bot).start()
alarm = Process(target = alarm_timer).start()
当我运行它时,控制台的输出是:
From timer: None
Bot Active
然后在尝试禁用警报时:
From subfile: False
From timer: None
因此,虽然在我的函数中注册了类更改。它不会进入课堂本身。
我究竟做错了什么?
Alarm.alarmpi.alarm_enabled
也许?