Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.3k views
in Technique[技术] by (71.8m points)

Unable to change a variable in running python program

I have a program running on several threads and I'm trying to change a variable self.pausing, through the telegram api, while the program is running. I made a function that changes the variable upon receiving a message:

from time import sleep
import threading

class program:
    
    def __init__(self):
        self.pausing = False
        
    def paus(self):
        if self.pausing == False:
            self.pausing = True
            print('self.pausing = True')
        elif self.pausing == True:
            self.pausing = False
            print('self.pausing = False')

    def run(self):
        while True:
            if self.pausing == False:
                sleep(2)
                print(self.pausing)
                sleep(2)
                print('Doing things...')
            elif self.pausing == True:
                print('Pausing..')
                sleep(2)
        
    def main(self):
        threading.Thread(target=telegram().main).start()
        threading.Thread(target=self.run).start()



class telegram:
    
    def __init__(self):
        pass
     
    def main(self):
        while True:
            sleep(8)
            print('A message was recieved!')
            program().paus()
                

When a message is received the function is run and prints successfully, but the variable self.pausing isn't changed.

I guess I'm misunderstanding something about self.* variables. My expected output would be the variable to be changed.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You span multiple instances of your classes you need to just spawn one of each.

Here is the working code that you can run:

from time import sleep
import threading

class program:
 
    def __init__(self):
        self.pausing = False
        
    def paus(self):
        if self.pausing == False:
            self.pausing = True
            print('self.pausing = True')
        elif self.pausing == True:
            self.pausing = False
            print('self.pausing = False')

    def run(self):
        while True:
            if self.pausing == False:
                sleep(2)
                print(self.pausing)
                sleep(2)
                print('Doing things...')
            elif self.pausing == True:
                print('Pausing..')
                sleep(2)
        
    def main(self):
        prog = self
        tel = telegram(prog)
        threading.Thread(target=tel.main).start()
        threading.Thread(target=prog.run).start()



class telegram:
    
    def __init__(self, prog):
        self.prog = prog
     
    def main(self):
        while True:
            sleep(2)
            print('A message was recieved!')
            self.prog.paus()


p = program()
p.main()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...