Take the 2-minute tour ×

I have a project that I'm writing in Python that will be sending hardware (Phidgets) commands. Because I'll be interfacing with more than one hardware component, I need to have more than one loop running concurrently.

I've researched the Python multiprocessing module, but it turns out that the hardware can only be controlled by one process at a time, so all my loops need to run in the same process.

As of right now, I've been able to accomplish my task with a Tk() loop, but without actually using any of the GUI tools. For example:

from Tk import tk

class hardwareCommand:
    def __init__(self):
        # Define Tk object
        self.root = tk()

        # open the hardware, set up self. variables, call the other functions

        # start the Tk loop

    def hardwareLoop(self):
        # Timed processing with the hardware

    def UDPListenLoop(self):
        # Listen for commands from UDP, call appropriate functions
        self.state = updateState(self.state)

    def eventListenLoop(self,event):
        if event == importantEvent:
            self.state = updateState(self.event.state)


So basically, the only reason for defining the Tk() loop is so that I can call the root.after() command within those functions that need to be concurrently looped.

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


share|improve this question
loop in daemon thread –  eri Aug 7 '13 at 16:56
Perhaps consider taking a look at gevent –  voithos Aug 7 '13 at 17:00
Have you looked at python threading (not multiprocess?) Things like locks and threads allow you to manage multiple threads and coordinate their access to particular pieces of hardware, etc. –  TheBigC Aug 7 '13 at 18:24
add comment

1 Answer

up vote 1 down vote accepted

The multiprocessing module is geared towards having multiple separate processes. Although you can use Tk's event loop, that is unnecessary if you don't have a Tk based GUI, so if you just want multiple tasks to execute in the same process you can use the Thread module. With it you can create specific classes which encapsulate a separate thread of execution, so you can have many "loops" executing simultaneously in the background. Think of something like this:

from threading import Thread

class hardwareTasks(Thread):

    def hardwareSpecificFunction(self):
        Example hardware specific task
        #do something useful

    def run(self):
        Loop running hardware tasks
        while True:
            #do something

class eventListen(Thread):

    def eventHandlingSpecificFunction(self):
        Example event handling specific task
        #do something useful

    def run(self):
        Loop treating events
        while True:
            #do something

if __name__ == '__main__':

    # Instantiate specific classes
    hw_tasks = hardwareTasks()
    event_tasks = eventListen()

    # This will start each specific loop in the background (the 'run' method)

    while True:
        #do something (main loop)

You should check this article to get more familiar with the threading module. Its documentation is a good read too, so you can explore its full potential.

share|improve this answer
Excellent description, thanks! –  user1636547 Aug 28 '13 at 21:50
add comment

Your Answer


