Game engine freezes with my socket communication script

Hail everyone!
I’ve got a quite anoying problem with my socket communication script…

This script is simple and working, but the game engine freezes at the launch of my script and until it ends… that’s quite annoying as I wanted my client to be able to interact in REAL TIME with my blender’s character.

I’ve already send a PM to Saluk as he seems to be quite experienced with socket, but I think that the answear may interest people.

This script is a controller of my hero’s squeleton and is used with an always sensor (doesn’t work better with a keyboard sensor).

Here’s my simplify script:

import sys, Blender, GameLogic, socket
cont=GameLogic.getCurrentController() HOST=‘127.0.0.1’
PORT = 50000
mySocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
mySocket.bind((HOST, PORT))
except socket.error:
print “warning connection problem ;)”
sys.exit
while 1:
print "ready to serve…:slight_smile: "
mySocket.listen(5)
connexion, adresse = mySocket.accept()
print “Client connected, adresse IP %s, port %s” % (adresse[0], adresse[1])

connexion.send(“everything is connected”)
msgClient = connexion.recv(1024)
while 1:
print “C>”, msgClient
if msgClient == “go” :
GameLogic.addActiveActuator( cont.getActuator(“go”),1)
GameLogic.addActiveActuator( cont.getActuator(“go”),0)
elif msgClient == “bye”:
break
connexion.send(“roger”)
msgClient=connexion.rescv(1024)
connexion.send(“bye”)
connexion.close()
break

If anybody has an idea…
If one of you speak of “threads”, please explain what does that means and how it works with python…

Arkis %|

I believe that blender execution stops until python execution ends…
(maybe with threads too?)

threads are kindof like seperate processes, and they allow more than one thing to be worked on at once. They are not any faster in execution, but if something is slow (loading a file for example) they can allow the rest of your program to contue running (or taking user input).

I don’t know if you can have persistent threads in the game engine.

(saluk would be the one to ask)

I believe that blender execution stops until python execution ends…

I think so, too.

msgClient = connexion.recv(1024)

You only receive one time. Think what happens if the command is transmitted during multiple frames.

while 1:
print “C>”, msgClient
if msgClient == “go” :
elif msgClient == “bye”:

What are you “whiling” for? Maybe change order of recv and while. Another thing that could happen is receiving two commands, what makes msgClient look like “gobye”.

Some ideas of implementation:

1st way:
Make it non blocking. The problem is, the script never ends. Python controllers script blocks blender. The way I use, it is fired every frame. You have to devide in two parts. Initialization and “communication”. During initialization you set up your socket (make it non blocking) an stuff. Make the socket part of the GameLogic dictionary (GameLogic.mysock = the_sock) to use it in the next frame. During communication you carry data each frame and buffer it somewhere (also GameLogic dictionary). After an end sign (or any other way of detect a whole transmission), you execute the commands so far.
That way it works quite nice for me, with a C++ python extension. Maybe some kind of busy waiting, but very easy to use.

2nd way:
Threads. Make the script run independent from controller after it is started.
Some tests with python threads give me errors and blender crashes, so I stop trying. You script attempt looks like that way.

If you habe any more questions about way 1 ask.

Thanks for replying!
Finally, I’ve made my python script client and it connects every time it is run to get the informations from a server… and it seems to be working, but it’s quite long to execute…