Blender - 3D view does not refresh with my python script I have an Arduino connected in serial with my Mac, it works, I get my datas in blender but the visualisation of the 3D view does not work.



import bge
import serial
import io
import mathutils
import math

def main():

#get info about the Brick
cont = bge.logic.getCurrentController()
own = cont.owner     

#Get sensor info, so we can run this script in loop
mainloop = cont.sensors["MainLoop"]

#initialization, run this only in the first loop
if not 'init' in own:
    own['init'] = 1 #create own['init'], so we know that we have done initialization and we do not run it again

    #initialize serial port
    own['serial_port'] = serial.Serial()
    own['serial_port'].baudrate = 9600
    own['serial_port'].port = '/dev/cu.usbmodem1431' #adjust to your port
    own['serial_port'].timeout=0.1 #you may want to play with this value, lower is, higher value e.g. 1 will slow down everything

    #needed for readline command (if you decide to use it)                               
    own['serial_io'] = io.TextIOWrapper(io.BufferedRWPair(own['serial_port'], own['serial_port']))

    #open the serial port
    own['serial_port'].close() #sometimes the port is stucked if not closed properly, force it to close
    own['serial_port'].open()
    print("Serial port Open = ",own['serial_port'].is_open)

    #start the loop
    mainloop.usePosPulseMode = True             

#Main loop        
answer = ""
quit = 0;
while quit&lt;2:  #wait in this while until we have at least one full valid data
    if own['serial_port'].in_waiting &gt; 0:
        byte_in = own['serial_io'].read(own['serial_port'].in_waiting)
        answer = answer + byte_in

        if answer.count('q')&gt;1:
            q=q+1
            print ("QUIT DETECTED")
            answer.replace('q','')

        if answer.count('#')&gt;1:
            answers = answer.split('#') # break the answer into pieces
            datas = answers[0].split(';')

            if len(datas)&gt;2:

                ownOrient = own.worldOrientation.to_euler()
                ownPosition = own.worldPosition
                ownOrient[0] = math.radians(float(datas[0])) # I used -1 as the brick was rotating then
                ownPosition[1] = float(datas[1])*0.1 # I used -1 as the brick was rotating then
                ownOrient[2] = math.radians(float(datas[2])) # I used -1 as the brick was rotating then
                ownMatrix = ownOrient.to_matrix() #build our new matrix so it can be applied to the object#
                own.worldOrientation = ownMatrix #set the new orientation

                own.worldPosition = ownPosition #set the new orientation
                //HERE IT SHOULD REDRAW THE 3D VIEW BUT NOTHING HAPPENS

main()


If someone knows why nothing happens when running the program, I would appreciate the help.

I should obtain something like this:
Ok I found the answer, 
The while loop is not the way to do it because it freeze the render until it get out of it. 
I have to code a class without this while loop