multithreading - Is this a valid way to subclass a python thread to accept a variable update? -


i'm looking @ threading module in python (version 3.4.3), , having difficulty finding way update variable in target function called. think create global variable share between main program , thread starting, found myself creating following subclass instead. seems work purposes, i'm curious if hack or if it's valid.

the goal here create separate thread regularly (and quietly) pings server, , updates specified widget status of update:

from tkinter import * import threading  class ipthread(threading.thread):     def __init__(self, ip=none, labelobj=none):         self.ip = ip         threading.thread.__init__(self, target=self.checkconnection, args=(labelobj,))      def newip(self, ip):         self.ip = ip      def checkconnection(self, widget):         while true:             self.response = os.system("ping -c 1 -w 10 " + self.ip + " > /dev/null 2> /dev/null")             if self.response==0:                 widget.config(text="connected", bg='green')             else:                 widget.config(text="no connection", bg='red')             time.sleep(1)  if __name__=="__main__":     win = tk()     status = label(win, text='')     status.pack()     ipchecker = ipthread(ip='192.168.1.1',widget=status)     time.sleep(10)     ipchecker.newip('192.168.1.2') 

while i've put simple routine here calls update after 10-second delay, in program thread initialized when create tkinter frame control panel. panel has button calls newip method update thread. works, i'm feeling i've accomplished something, overkill or unnecessary? couldn't find way initialize "checkconnection" routine separate thread, , able update ip address using.

you ask several questions, i'll answer them all.

is valid way pass arguments thread?

there 2 ways pass arguments thread:

  • subclass thread, redefine function run , access new variables there, in

    class ipthread(threading.thread):   def __init__(self, ip=none, labelobj=none):     self.ip = ip     self.labelobj = labelobj    def newip(self, ip):     self.ip = ip    def run(self):     while true:         self.response = os.system("ping -c 1 -w 10 " + self.ip + " > /dev/null 2> /dev/null")         if self.response==0:             self.labelobj.config(text="connected", bg='green')         else:             self.labelobj.config(text="no connection", bg='red')         time.sleep(1) 
  • or specify target call

    def checkconnection(widget, ip):     while true:         self.response = os.system("ping -c 1 -w 10 " + self.ip + " > /dev/null 2> /dev/null")         if self.response==0:             widget.config(text="connected", bg='green')         else:             widget.config(text="no connection", bg='red')         time.sleep(1)  th = threading.thread(target=checkconnection, args=(labelobj, ip)) th.start() 

is proper solution problem?

no, not!

widget libraries not second thread cut across widgets. use tkinter timer instead of thread, or use custom event let thread tell widget text has changed.

when using timer, gui becomes unresponsive while tick being handled. in case, ping has unbounded delay keep thread , let send custom event.


Comments

Popular posts from this blog

java - Andrioid studio start fail: Fatal error initializing 'null' -

android - Gradle sync Error:Configuration with name 'default' not found -

StringGrid issue in Delphi XE8 firemonkey mobile app -