title: Run bleak (python library) in background with asyncio
body: <p>I want to use the bleak library in Python to receive data from a Bluetooth Low Energy device. This part is working. My problem is now, that I don't know how to run this code in the background or parallel.</p>
  <p>Eventually, I want to build a tiny python app which is processing the data from the Bluetooth device. So bleak is looping all the time fetching data from a bluetooth device and sending it to the main process where it is processed and displayed.</p>
  <p>For some reason, bleak does not run in a thread. Is it possible to use asyncio for this (since it is already used by bleak maybe a good way to go)?</p>
  <p>I checked out threads and multiprocessing but somehow I found only examples without processes which loop infinitely and send data. I'm totally new to the topic of parallelization and/or asynchronous processes. Maybe one of you can give a hint where to look for a proper solution for this case.</p>
  <p>Below is my code so far (for now I just loop and print data).</p>
  <pre><code>from bleak import BleakClient
  import json
  import time
  
  current_index = 0
  
  time_array = [0] * 20
  
  
  def TicTocGenerator():
      # Generator that returns time differences
      ti = 0           # initial time
      tf = time.time() # final time
      while True:
          ti = tf
          tf = time.time()
          yield tf-ti # returns the time difference
  
  TicToc = TicTocGenerator() # create an instance of the TicTocGen generator
  
  # This will be the main function through which we define both tic() and toc()
  def toc(tempBool=True):
      # Prints the time difference yielded by generator instance TicToc
      tempTimeInterval = next(TicToc)
      global current_index
      if tempBool:
          #print( &quot;Elapsed time: %f seconds.\n&quot; %tempTimeInterval )
          time_array[current_index] = tempTimeInterval
          if current_index == 19:
              current_index = 0
          else:
              current_index += 1
  
  
  def tic():
      # Records a time in TicToc, marks the beginning of a time interval
      toc(False)
  
  
  def Average(lst): 
      return sum(lst) / len(lst) 
  
  
  #address = &quot;30:ae:a4:5d:bc:ba&quot;
  address = &quot;CCA9907B-10EA-411E-9816-A5E247DCA0C7&quot;
  MODEL_NBR_UUID = &quot;beb5483e-36e1-4688-b7f5-ea07361b26a8&quot;
  
  
  
  async def run(address, loop):
      async with BleakClient(address, loop=loop) as client:
          while True: 
              tic()
              model_number = await client.read_gatt_char(MODEL_NBR_UUID)
              toc()
              json_payload=json.loads(model_number)
              print()
              print(json_payload)
              print(&quot;Temp    [°C]: &quot;+&quot;{:.2f}&quot;.format(json_payload[&quot;Temp&quot;]))
              print(&quot;Volt     [V]: &quot;+&quot;{:.2f}&quot;.format(json_payload[&quot;Volt&quot;]))
              print(&quot;AngX:         &quot;+str(json_payload[&quot;AngX&quot;]))
              print(&quot;AngY:         &quot;+str(json_payload[&quot;AngY&quot;]))
              print(&quot;AngZ:         &quot;+str(json_payload[&quot;AngZ&quot;]))
              #print(&quot;Millis: {0}&quot;.format(&quot;&quot;.join(map(chr, model_number))))
              print(&quot;Average [ms]: {:.1f}&quot;.format(Average(time_array)*1000))
  
  
  loop = asyncio.get_event_loop()
  loop.run_until_complete(run(address, loop))
  </code></pre>