Multiple python loops in same process -


i have project i'm writing in python sending hardware (phidgets) commands. because i'll interfacing more 1 hardware component, need have more 1 loop running concurrently.

i've researched python multiprocessing module, turns out hardware can controlled 1 process @ time, loops need run in same process.

as of right now, i've been able accomplish task tk() loop, without using of gui tools. example:

from tk import tk  class hardwarecommand:     def __init__(self):         # define tk object         self.root = tk()          # open hardware, set self. variables, call other functions         self.hardwareloop()         self.udplistenloop()         self.eventlistenloop()          # start tk loop         self.root.mainloop()       def hardwareloop(self):         # timed processing hardware         sethardwarestate(self.state)         self.root.after(100,self.hardwareloop)       def udplistenloop(self):         # listen commands udp, call appropriate functions         self.state = updatestate(self.state)         self.root.after(2000,self.udplistenloop)       def eventlistenloop(self,event):         if event == importantevent:             self.state = updatestate(self.event.state)         self.root.after(2000,self.eventlistenloop)  hardwarecommand() 

so basically, reason defining tk() loop can call root.after() command within functions need concurrently looped.

this works, there better / more pythonic way of doing it? i'm wondering if method causes unnecessary computational overhead (i'm not computer science guy).

thanks!

the multiprocessing module geared towards having multiple separate processes. although can use tk's event loop, unnecessary if don't have tk based gui, if want multiple tasks execute in same process can use thread module. can create specific classes encapsulate separate thread of execution, can have many "loops" executing simultaneously in background. think of this:

from threading import thread  class hardwaretasks(thread):      def hardwarespecificfunction(self):         """         example hardware specific task         """         #do useful         return      def run(self):         """         loop running hardware tasks         """         while true:             #do             hardwarespecifictask()   class eventlisten(thread):      def eventhandlingspecificfunction(self):         """         example event handling specific task         """         #do useful         return      def run(self):         """         loop treating events         """         while true:             #do             eventhandlingspecificfunction()   if __name__ == '__main__':      # instantiate specific classes     hw_tasks = hardwaretasks()     event_tasks = eventlisten()      # start each specific loop in background (the 'run' method)     hw_tasks.start()     event_tasks.start()      while true:         #do (main loop) 

you should check this article more familiar threading module. documentation read too, can explore full potential.


Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -