0

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?

Keva161
  • 2,623
  • 9
  • 44
  • 68
  • 1. bot and alarm are in two different processes. They will each have their own instance of and Alarm() object. – Alex L Feb 04 '17 at 12:22
  • 2. FWIW, alarm_enabled is an instance variable, not a class variable. – Alex L Feb 04 '17 at 12:25
  • Drat, I keep hitting the return key and it keeps making new comments. 3. When submitting code examples, it's best to ensure that they are capitalized and (in python especially!) indented right--which is to say it's best to ensure that they are running code examples! – Alex L Feb 04 '17 at 12:29
  • So by your first comment, you are saying its giving me a false negative? I've been trying to fix this for hours :D – Keva161 Feb 04 '17 at 12:34
  • I'm saying that because they're running in two separate processes, it'll be like there are two different universes. In the alarm_timer universe, alarmpi.alarm_enabled remains None forever. – Alex L Feb 04 '17 at 17:42
  • So how do I have my alarm settings in a separate file but still have them accessible in my bot file? Isn't the import statement sufficient? – Keva161 Feb 04 '17 at 17:46

0 Answers0