threads, sockets, pyopengl, pickle (and tkinter...)

Rod Stephenson viking_kiwi at yahoo.poofters.com
Tue Jun 22 05:37:05 CEST 2004


I am developing an engineering application for modelling multiphase
flows in large pipe networks, and have just discovered the joy of
pyopengl for 3D visualization. However the incantation I'm using -
with glut on Windows - doesnt coexist well with other python gui's -
glutMainLoop() appears not to work alongside tkinter.mainloop(). (I
tried putting them in separate threads but no success there either)
The issue has been raised in various discussions here in the past with
no satisfacory resolution as far as I can see, though Togl has been
suggested - but all I can find on the project page is c-source code,
havent managed to get the demo Togl stuff in the pyopengl installation
to work.

Anyway, I've tried another avenue of attack - running the
visualization portion in a separate application to the
design/computation part, and communicating by sockets, this has a
useful advantage that the two parts of the application can run on
different machines if needed.

So when I want to update the display, the design app packages up the
drawing data into a display list (just a python list in fact
with each entry an instruction to draw a particular element, colors
etc), then pickles the thing up and send it through the socket.

At the other end the display app reads from the socket - this runs in
a separate thread, unpickles the data and puts it in a global variable
which the gl drawing function can access. The two relevant routines
and the stuff to fire everything off are appended. (doGlutMain() sets
up all the gle stuff and sets glutDrawFunction() with DrawStuff)

This *almost* works, except that things block until I generate some
event in the OpenGL display window - even just giving it the focus
after sending off some data. I'm not quite sure how to rectify this -
any suggestions would be appreciated. Perhaps doing something with the
glutIdleFunction()? And am I doing the right thing with the lock stuff?

Finally could someone confirm that pickled data never contains a "."
character apart at the end of the string (I use this as a delimiter in
the socket communiction.) There was an old posting (from Guido
himself) that seemed to imply this.

Thanks

rs


# draw routine
def DrawStuff():
    global lock
    glPushMatrix()
    someGLStuff()    
    lock.acquire()
    if dList: 
        for  z in dList:
            draw(z)
    lock.release()
    glPopMatrix ()
    glutSwapBuffers ()


#  Server program
def getData():
    global lock, dList
    HOST = ''       
    PORT = 50007  
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind((HOST, PORT))
    s.listen(1)
    conn, addr = s.accept()
    try:
        while 1:
            data=""
            while data[-1:] != ".":
                data += conn.recv(4096)
            try:
                z = cPickle.loads(data)
                lock.acquire()
                dList  = z
                lock.release()
                glutPostRedisplay()
                print len(z)
                conn.send("OK")
            except:
                conn.send("rsnd")
    except:
        conn.close()


thread.start_new_thread(getData, ())
time.sleep(.1)
doGlutMain(DrawStuff)







-- 
Bruce Rule 1. 



More information about the Python-list mailing list