title: Improving client latency for receiving multiple data blocks parallely from the server using buffer using Python
body: <p>In order to increase the latency of my client, which is receiving data blocks from a server (in my case a software whose sampling rate in 100Hz) I plan to use a buffer. Since my client is not only receiving the data blocks but processing it and giving a real time value (almost) of the result.</p>
  <p>Currently, my client code takes 0.1s to receive and process the data blocks.
  With the buffer in the client side, I intend to continuously receive the data blocks and in parallel read out the latest data block to process it. This way I make sure that I am processing every data block that the software is sending.</p>
  <p>Could someone tell me how to implement a FIFO and in parallel compute in the client side? And if there is a better approach than doing this?</p>
  <p>Thank you in advance!</p>
  <p>The following is my client code:</p>
  <pre><code>
  import socket
  import turtle
  import timeit
  from tkinter import *
  root = Tk()
  canvas = Canvas(root, width = 150, height = 50)
  canvas.grid(row = 4, column = 3)
  input_label = Label(root, text = &quot;Input all the gratings set straight wavelength values in nm&quot;)
  input_label.grid(row = 0)
  
  light_label = Label(root, text = 'Status')
  light_label.grid(row = 3, column = 3)
  
  # Green light
  green_light = turtle.RawTurtle(canvas)
  green_light.shape('circle')
  green_light.color('grey')
  green_light.penup()
  green_light.goto(0,0)
  
  # Red light
  red_light = turtle.RawTurtle(canvas)
  red_light.shape('circle')
  red_light.color('grey')
  red_light.penup()
  red_light.goto(40,0)
  
  
  
  
  core_string = &quot;Core &quot;
  entries = []
  
  label_col_inc = 0
  entry_col_inc = 1
  core_range = range(1, 5)
  
  for y in core_range:
      core_text = core_string + str(y) + '_' + '25'
      core_label = Label(root, text = core_text)
      entry = Entry(root)
      core_label.grid(row=1, column=label_col_inc, sticky=E)
      entry.grid(row=1, column=entry_col_inc)
      entries.append(entry)
      label_col_inc += 2
      entry_col_inc += 2
  
  threshold_label = Label(root, text = &quot;Threshold in nm&quot;)
  entry_threshold = Entry(root)
  
  threshold_label.grid(row = 2, sticky = E)
  entry_threshold.grid(row = 2, column = 1)
  
  #dataNo_label = Label(root, text = &quot;Number of data to be collected&quot;)
  #entry_datNo = Entry(root)
  
  #dataNo_label.grid(row = 3, sticky = E)
  #entry_datNo.grid(row = 3, column = 1)
  
  # function to receive TCP data blocks
  def getData():
      start = timeit.default_timer()
      host = '127.0.0.1'
      port = 5000
      s = socket.socket()
      s.connect((host, port))
  
      len_message = s.recv(4)
  # input of the number of data to be received
      #dataNo = float(entry_datNo.get())
  
      while len_message:
          #bytes_length = int.from_bytes(len_message,'big') # for the software
          bytes_length = int(len_message.decode('utf-8')) # for the self-made server
          data = s.recv(bytes_length)
          stringdata = data.decode('utf-8')
          rep_str = stringdata.replace(&quot;,&quot;, &quot;.&quot;)
          splitstr = rep_str.split()
  
          # received wavelength values
          inc = 34
          wav_threshold = []
          for y in entries:
              straight_wav = float(y.get())
              wav = float(splitstr[inc])
              wav_diff = wav - straight_wav
              if wav_diff &lt; 0:
                  wav_diff = wav_diff * (-1)
              wav_threshold.append(wav_diff)
              inc += 56
  
          threshold = float(entry_threshold.get())
  
          # writing into the file
          data = []
          inc1 = 0
          col1 = 2
          col2 = 6
  
          data.insert(0, (str(splitstr[0])))
          data.insert(1, (str(splitstr[1])))
  
          for x in wav_threshold:
              if (x &gt; threshold):
                  red_light.color('red')
                  green_light.color('grey')
                  data.insert(col1, (str(splitstr[34 + inc1])))
                  data.insert(col2,(str(x)))
              else:
                  red_light.color('grey')
                  green_light.color('green')
                  data.insert(col1,'-')
                  data.insert(col2,'-')
              inc1 += 56
              col1 += 1
              col2 += 1
  
          write_file(data)
          len_message = s.recv(4)
  
      else:
          print('out of loop')
          stop = timeit.default_timer()
          print('Time: ', stop - start)
      s.close()
  
  # function to write into the file
  def write_file(data):
      with open(&quot;Output.txt&quot;, &quot;a&quot;) as text_file:
          text_file.write('\t'.join(data[0:]))
          text_file.write('\n')
  
  
  data_button = Button(root, text = &quot;Get data above threshold&quot;, command = getData)
  data_button.grid(row = 5, column = 0)
  
  root.mainloop()
  
  
  
  </code></pre>