title: Use of asyncio event loop recursively
body: <p>I am new to asyncio ( used with python3.4 ) and I am not sure I use it as one should. I have seen in <a href="https://stackoverflow.com/questions/5177439/postponing-functions-in-python/14040516#14040516">this thread</a> that it can be use to execute a function every n seconds (in my case ms) without having to dive into threading.</p>
  
  <p>I use it to get data from laser sensors through a basic serial protocol every n ms until I get m samples. </p>
  
  <p>Here is the definition of my functions :</p>
  
  <pre><code>def countDown( self, 
                 loop, 
                 funcToDo, 
                 *args, 
                 counter = [ 1 ],
                 **kwargs ):
      """ At every call, it executes funcToDo ( pass it args and kwargs )
          and count down from counter to 0. Then, it stop loop """
      if counter[ 0 ] == 0:
          loop.stop() 
      else:
          funcToDo( *args, **kwargs )
          counter[ 0 ] -= 1
  
  
  def _frangeGen( self, start = 0, stop = None, step = 1 ):
      """ use to generate a time frange from start to stop by step step """
      while stop is None or start &lt; stop:
          yield start
          start += step
  
  def callEvery( self, 
                 loop, 
                 interval, 
                 funcToCall, 
                 *args, 
                 now = True, 
                 **kwargs ):
      """ repeat funcToCall every interval sec in loop object """
      nb = kwargs.get( 'counter', [ 1000 ] )
      def repeat( now = True,
                  times = self._frangeGen( start = loop.time(),
                                           stop=loop.time()+nb[0]*interval,
                                           step = interval ) ):
          if now:
              funcToCall( *args, **kwargs )
          loop.call_at( next( times ), repeat )
  
      repeat( now = now )
  </code></pre>
  
  <p>And this is how I use it (getAllData is the function that manage serial communication) :</p>
  
  <pre><code>ts = 0.01
  nbOfSamples = 1000
  loop = asyncio.get_event_loop()
  callEvery( loop, ts, countDown, loop, getAllData, counter = [nbOfSamples] )  
  loop.run_forever()
  </code></pre>
  
  <p>I want to put that bloc into a function and call it as often as I want, something like this :</p>
  
  <pre><code>for i in range( nbOfMeasures ):
      myFunction()
      processData() 
  </code></pre>
  
  <p>But the second test does not call getAllData 1000 times, only twice, sometimes thrice. The interesting fact is one time in two I get as much data as I want. I don't really understand, and I can't find anything in the docs, so I am asking for your help. Any explanation or an easier way to do it is gladly welcome :)</p>