multiprocessing and pyglet

761 views
Skip to first unread message

Gabriele Lanaro

unread,
Nov 5, 2012, 3:21:06 PM11/5/12
to pyglet...@googlegroups.com
I've tried several approaches to run pyglet in parallel with the executing code. One (partially) successful solution was to simply run pyglet-related stuff into a mutliprocessing.Process and communicating using a sync primitive such as a Queue. The problem I'm having is that this approach doesn't play nicely with all the drivers, a test code like the following, works nicely on 'radeon' open source drivers, but it doesn't work on proprietary nvidia drivers (the subprocess sort-of crashes at line 6). If I remove the call to Batch (or any other opengl-related call) it works with both video cards/drivers. Do you think there's an explanation for this? I'd prefer avoiding to restrict all references to opengl in just one process the reason is that I basically cache shaders in the main process. I was wondering if there exist some way to prevent this kind of problems.
--------------------------
import pyglet
import multiprocessing
b = pyglet.graphics.Batch()

def inprocess():
    w = pyglet.window.Window()
    pyglet.app.run()

p = multiprocessing.Process(target=inprocess)
p.start()
p.join()
---------------------------

Tristam MacDonald

unread,
Nov 5, 2012, 3:25:27 PM11/5/12
to pyglet-users
It's a general limitation of OpenGL, that calls to OpenGL can only occur from a single thread. There is no easy way to work around that limitation, pre OpenGL version 4+.



--
You received this message because you are subscribed to the Google Groups "pyglet-users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/pyglet-users/-/SOjRn2XTbF8J.
To post to this group, send email to pyglet...@googlegroups.com.
To unsubscribe from this group, send email to pyglet-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/pyglet-users?hl=en.



--
Tristam MacDonald
Software Development Engineer, Amazon.com

Nathan

unread,
Nov 5, 2012, 6:23:22 PM11/5/12
to pyglet...@googlegroups.com
I don't understand why this approach wouldn't work in general.  The python multiprocessing module explicitly uses subprocess, not threads.  Separate processes (even if spawned by each other) should each be able to handle their own OpenGL contexts.

Perhaps the problem is that multiprocessing is forking the current process, which probably means that you need to make sure all OpenGL-related stuff is started _after_ the separate process is running.  Why don't you try something like this and see how it goes?

import multiprocessing

def inprocess():
    import pyglet
    b = pyglet.graphics.Batch()

    w = pyglet.window.Window()
    pyglet.app.run()

p = multiprocessing.Process(target=inprocess)
p.start()
p.join()

~ Nathan

Lord Duck

unread,
Nov 6, 2012, 4:06:51 AM11/6/12
to pyglet...@googlegroups.com
I've been tinkering around with multiprocessing and pyglet a few weeks ago, using a process which imports pyglet after construction like Nathan's example. I have not run into any problems so far. Also, keep in mind that under windows you'll need to add
if __name__ == '__main__':

2012/11/6 Nathan <nathan...@gmail.com>

Gabriele Lanaro

unread,
Nov 6, 2012, 5:35:38 PM11/6/12
to pyglet...@googlegroups.com

If I do the pyglet initialization in the new process it seems to work. It seems sufficient to do a reload(pyglet) in the new process. I'm not 100% sure this will work on my system because now I've found another very nasty bug that causes random freezes (but this will be in another thread). Thank you everybody for all the help!

Reply all
Reply to author
Forward
0 new messages