Non blocking server

Hi everyone,

First of all let me thank you guys for the great help you are providing here.
I have a (hopefully easy) problem. I want to create a server in Blender, which can be started with a script and runs in the background. A client from outside of blender should be able to send commands to the server (e.g. to manipulate a scene). The problem is, the GUI is always freezing when i run the server and when it is waiting for messages (recvfrom). Unfortunately, although many threads already exist on this subject, i was unable to find the solution (Let me state here i both lack experience in network programming and blender).
Declaring the socket a non-blocking socket didnt do the trick, and I read in another thread that multithreading in blender with python is leading to random crashes.
Is there another way to apply commands from an external source to an open blender scene? Or is my current code flawed? (Likely… it is mostly a patchwork monster created from different sources :cool:)

import socket
e_udp_sock = socket.socket( socket.AF_INET, socket.SOCK_DGRAM ) 
e_udp_sock.setblocking(0) #make this server into a non-blocking socket
e_udp_sock.bind( (Quelle,Port) ) #IP-Adresse und Port werden durch bind miteinander verkn�pft.
print ('########my Server########') 
print ('Source=',Quelle)
print ('Port=',Port)
def receive():
    while 1:
            data, addr = e_udp_sock.recvfrom( 1024 )# Puffer-Gr��e ist 1024 Bytes. 
                    # Die Puffergr��e muss immer eine Potenz
                    # von 2 sein
            print ("received Message:", data)
            print ("Client:", addr) # Adresse besteht aus IP und Port
        except socket.error:
receive() #Programm wartet in einer Endlosschleife auf eingehende Nachrichten, Herkunftsort ist egal.

Thank you in advance for your support :slight_smile:

Don’t use “while 1:” , fire up a thread instead.
You can do multithreading in Blender Python, as long as you don’t do anything Blender-related in the network thread (everything starting with bpy is a no-go).
Your network thread should just listen for commands and dump them in some kind of queue. Your main thread (probably a modal operator) should check if new data are in the queue, and process them.