python threads operating serially
Question: I am a python noob. I am trying to design a visual stimulus for experiments in
my lab. The stimulus should stop when the user presses a key. The entire
experiment is timing sensitive and so I cannot run the key check serially.

The code that I wrote looks like this.

* * *
    
    
    class designStim:
    '''
    This is class to hold functions designing the stimulus. There are other functions
    in this class but the only one that is called is the following
    '''
    
        def deisgnStart(self)
            #This function returns a dictionary variable containing 
            #the raw stimulus
    

* * *
    
    
    class dispStim(threading.Thread):
    
        def __init__(self,stimdata,q):
            threading.Thread.__init__(self)
    
        #*Assign the other data from stimdata to self*
        def run(self):
            #Code to run the stimulus
    

* * *
    
    
    class checkKbdAbort(threading.Thread):
    
        def __init__(self,q):
            threading.Thread.__init__(self)
    
        def run(self):
            #Code to check for keyboard key press. In case of key press, 
            #use the queue to abort     dispStim*
    
    
    if __name__ == '__main__':
    
        a=designStim('test')
        stimdata=a.designStart()
        q=Queue()
        thread1=dispStim(stimdata,q)
        thread2=checkKbdAbort(q)
        thread1.start()
        thread2.start()
    

* * *

This code works when I run serially which leads me to believe that my display
scripts are correct. However, when I run the code in this form, the two
threads are not run in parallel. Thread1 executes and then thread2 runs.
During the thread1 run, the stimulus does not display. Is there a mistake I am
making during the class initialization/call? Please help

Thank you

Mathew


Answer: While I cannot answer your question for sure (since I don't see a complete
example and thus do not know why things seem to be executing serially), and
putting aside what appear to be typos in the code copied into your question,
what you are attempting to do appears basically correct. If you subclass
threading.Thread and provide a run() definition, and if you launch each thread
instance via start(), the threads should run in parallel. (Thread 1 _may_ get
a small head start since it is kicked off first from the parent thread, but
I'm going to guess that it does not matter in this case.)

Here's a working example that shows where threads should be working in
parallel:

    
    
    import sys
    import threading
    
    # Using sys.stdout.write() instead of print; it seems nicer for
    # demoing threads in CPython :)
    
    class Fizz(threading.Thread):
        def run(self):
            for i in xrange(10):
                sys.stdout.write("fizz\n")
    
    class Buzz(threading.Thread):
        def run(self):
            for i in xrange(10):
                sys.stdout.write("buzz\n")
    
    fizz = Fizz()
    buzz = Buzz()
    fizz.start()
    buzz.start()
    

And here's what my output looks like:

    
    
    C:\Users\Vultaire>test.py
    fizz
    fizz
    fizz
    fizz
    fizz
    buzz
    buzz
    buzz
    buzz
    fizz
    buzz
    fizz
    fizz
    buzz
    fizz
    fizz
    buzz
    buzz
    buzz
    buzz
    

The threads in your example appear to be started in this way, so they _should_
be running in parallel. I'm guessing the problem is somewhere else? Or perhaps
the threads are running in parallel and just didn't seem like it? (Please
provide a more complete code example if you need further help! :))

