[Python/Threading]Would it be possible

Using this : http://docs.python.org/dev/library/threading.html#module-threading

I’m making a chat box. The gui and scripts are done, now I’m working on the server.

Based on my script the threads function would be:

1st thread: BGE scripts + Server (send Data)

2nd thread: Server (receive data)

The second thread would always be waiting for data.

By the way, this is my server script: http://www.pasteall.org/30136/python

Sure, anythings possible. However, as far as i’m aware, threading is a bit of a tricky issue in python / Blender. As soon as you run the thread, you sort of leave it to run on its own.
In most cases you won’t need to thread. If you’re worried about lag, that may be the case - but only running the server in a separate thread, as the client will have to run at game speed anyway to perform useful functions. Plus, i don’t beleive you’d have access to the BGE module inside a thread.


Thanks for the quick answer.

I’d run the server in a different process. This allows you to take advantage of computers with multiple cores. Python threads are stuck to a single core due to the GIL.

As far as I understand, the BGE “forces” you to use 1 processor… I think this is the cause of the slowdowns with networking, notably, BGE servers. That might be because the BGE might give priority to rendering!
So I’m counting in adding multitread to my servers in order to handle groups of clients separately. I was hoping to see that in action in your script…
I wouldn’t advice splitting the sending and receiving data, since they don’t usually run in parallel, but more on a ping pong way…eerrr, I think it does. Well, for a chat box, I really don’t know how you intend to exchange the data, and I’m also here to learn, networking is very new to me!

I’m trying something with different python module.
I will post when I’ll get results.

Ok so I found that multiprocessing could be a good idea (thanks Moguri).

So I create a new process in blender that receive and send data:

The code would look like that (hasn’t tested yet):
Still looking at the docs…

from multiprocessing import Process

def f(a):
    while 1:        
        data = server.recv('','266311','500',10)
        if data == 'Someting':

if __name__ == '__main__':
    p = Process(target=f, args=('a',))

You do not need Blender for a sever.

It does not even need to be Python.
You can run the server as a standalone application.
A server usually does not run at the same hardware as the client.

1:For simplicity sake, I prefer to use a python server.

2:The idea is that the host, to init a chat room, click on a button and set is username. Once everything is ok. The program execute a new process that will handle everything (aka: the server). Simple, fast and user friendly.

3:I know but I was thinking: The server is on the host computer and once running ( if the chat room is not private) it send the IP of the server to a list on some domain. The chat room read this list and create a list of available server that can be joined.When the server is closed , it remove the ip address from the list.