python threading and timing



hello

I'm writing a Python application for live music performance/improivsation,
using csound as the synthesis engine, and Python for compositional
algorithms, GUI and control.

I creating several threads:
one for GUI
one for csound,
one for a timed queue (timed automation events),
one for communication with a tcp based sensor interface.

My threads do not share data,
I use threads merely to enable several processes to run in paralell.
I think I do not need to use locks to control threads in this case,
but I'm not sure (?)

Also, I wonder what method I should be using to get a precise timing
in my automation thread (acting as a sequencer).

I've attached two simple examples of different ways I could implement threads.
The only task in these examples is to generate timed counters, but the
general procedure would be the same for more complex timed tasks.
One example uses time.sleep() to release a thread's interpreter lock,
and at the same time provide the time interval until the next
iteration.
The other example uses a thread event wait() call.

It seems that the example using wait() does not provide the same
precision as time.sleep() does (the counters does not sync, e.g. a
counter with a time interval of 0.1 does not count 10 times as fast as
a counter with a time interval of 1 second.

Any insights on how to do this the best way (thread safety and time
precision) would be greatly appreciated.

best
Oeyvind Brandtsegg
# test of threading routines for calling functions periodically

import threading
import time

class CounterTest(threading.Thread):
def __init__(self, res, name):
self.name = name
self.counter = 0
self.keepRunning = 1
self.res = res
threading.Thread.__init__(self)
def run(self):
while self.keepRunning:
print self.name, self.counter
self.counter += 1
time.sleep(self.res)
def stopme(self):
self.keepRunning = 0

c1 = CounterTest(1,'one')
c2 = CounterTest(0.1,'two')

print 'starting threads'
c1.start()
c2.start()

#allow threads to run for a while
time.sleep(5)
c1.stopme()
c2.stopme()

print 'threads stopped'# test of threading routines for calling functions periodically

import threading
import time

class CounterTest(threading.Thread):
def __init__(self, res, name):
self.name = name
self.counter = 0
self._stopevent = threading.Event()
self._sleepperiod = res
threading.Thread.__init__(self)
def run(self):
while not self._stopevent.isSet():
print self.name, self.counter
self.counter += 1
self._stopevent.wait(self._sleepperiod)
def stopme(self):
self._stopevent.set()

c1 = CounterTest(1,'one')
c2 = CounterTest(0.1,'two')

print 'starting threads'
c1.start()
c2.start()

#allow threads to run for a while
time.sleep(5)
c1.stopme()
c2.stopme()

print 'threads stopped'

Relevant Pages

  • [PATCH][2.6.7-mm7] perfctr documentation update
    ... For detailed hardware control register layouts, ... accesses the perfctr state from a forbidden thread. ... driver to form the value for the CESR. ... interrupt-mode counters was last suspended ...
    (Linux-Kernel)
  • what is the best practice to separate Pygtk and long running thread code
    ... I would like also to separate the gui part to the action part so that I ... message using a queue every second also. ... def process: ... #@-- window window1 { ...
    (comp.lang.python)
  • Re: best way to get data into a new instance?
    ... James Stroud wrote: ... I'm trying to construct it in a way that is independent of, say, the GUI interface that will be used, but I can't help but think that if all the data is entered into a GUI, then passed programmatically to the class, then it's no big deal because it happens behind the scenes. ... Its probably better to init with the bare minimum parameters and focus on how to move information from an external source into an already created instance: ... def set_value: ...
    (comp.lang.python)
  • More on Tk event_generate and threads
    ... There have been a number of posts about calling gui methods from other ... passed in through queue and using a polling loop with after methods. ... def run: ...
    (comp.lang.python)
  • Re: tkinter + interrupts
    ... def start: ... since the GUI is not *always* active; ... So clicking the 'Stop' button may have a one second delay before being ...
    (comp.lang.python)