I'm trying to enable/disable a class variable. I can see the change in my function when I call the disable function. But outside of the function, it still reads as enabled.
alarm.py
Class Alarm:
def __init__(self):
self.alarm_enabled = None
def alarm_toggle(self, status):
self.alarm_enabled = status
telegram_bot.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)
main.py
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()
The output to the console when I run this is:
From timer: None
Bot Active
Then when trying to disable the alarm:
From subfile: False
From timer: None
So while the class change is being registered in my function. It's not going through to the class itself.
What am I doing wrong?
Alarm.alarmpi.alarm_enabled
maybe?