Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"pyglet.gl.ContextException: Could not create GL context" when cocos imported in main process #281

Closed
buxx opened this issue Nov 20, 2016 · 2 comments

Comments

@buxx
Copy link
Contributor

buxx commented Nov 20, 2016

There is pyglet bug if we import cocos outside "real" process:

This is not working:

from multiprocessing import Process
import cocos

class HelloWorld(cocos.layer.Layer):
    def __init__(self):
        super().__init__()

        label = cocos.text.Label(
            'Hello, world',
            font_name='Times New Roman',
            font_size=32,
            anchor_x='center', anchor_y='center'
        )
        label.position = 320, 240
        self.add(label)


def test_cocos():
    cocos.director.director.init()
    hello_layer = HelloWorld()
    main_scene = cocos.scene.Scene(hello_layer)
    cocos.director.director.run(main_scene)


p = Process(
    target=test_cocos
)
p.start()
p.join()

And produce

Process Process-1:
Traceback (most recent call last):
File "/usr/lib/python3.5/multiprocessing/process.py", line 249, in _bootstrap
self.run()
File "/usr/lib/python3.5/multiprocessing/process.py", line 93, in run
self._target(*self._args, **self._kwargs)
File "c.py", line 21, in test_cocos
cocos.director.director.init()
File "/home/bux/Projets/synergine2/venv3.5/lib/python3.5/site-packages/cocos/director.py", line 327, in init
self.window = window.Window(*args, **kwargs)
File "/home/bux/Projets/synergine2/venv3.5/lib/python3.5/site-packages/pyglet/window/xlib/init.py", line 163, in init
super(XlibWindow, self).init(*args, **kwargs)
File "/home/bux/Projets/synergine2/venv3.5/lib/python3.5/site-packages/pyglet/window/init.py", line 515, in init
context = config.create_context(gl.current_context)
File "/home/bux/Projets/synergine2/venv3.5/lib/python3.5/site-packages/pyglet/gl/xlib.py", line 186, in create_context
return XlibContextARB(self, share)
File "/home/bux/Projets/synergine2/venv3.5/lib/python3.5/site-packages/pyglet/gl/xlib.py", line 296, in init
super(XlibContext13, self).init(config, share)
File "/home/bux/Projets/synergine2/venv3.5/lib/python3.5/site-packages/pyglet/gl/xlib.py", line 199, in init
raise gl.ContextException('Could not create GL context')

pyglet.gl.ContextException: Could not create GL context

But this don't procduce errors:

from multiprocessing import Process

def test_cocos():
    import cocos

    class HelloWorld(cocos.layer.Layer):
        def __init__(self):
            super().__init__()

            label = cocos.text.Label(
                'Hello, world',
                font_name='Times New Roman',
                font_size=32,
                anchor_x='center', anchor_y='center'
            )
            label.position = 320, 240
            self.add(label)

    cocos.director.director.init()
    hello_layer = HelloWorld()
    main_scene = cocos.scene.Scene(hello_layer)
    cocos.director.director.run(main_scene)


p = Process(
    target=test_cocos
)
p.start()
p.join()

Maybe it can be fixed (really initialize pyglet when used) ? Or speak about in doc (if it's not already done ... :s)

@ccanepa
Copy link
Contributor

ccanepa commented Mar 26, 2017

Added to the docs

@ccanepa ccanepa closed this as completed Mar 26, 2017
@crizCraig
Copy link

crizCraig commented Jul 17, 2018

Same problem and same fix with pyglet in general:

from multiprocessing import Process
import time

def test_window():
    import pyglet   #  Must import pyglet here in the process with your window initialization!
    window = pyglet.window.Window()
    time.sleep(5)


if __name__ == '__main__':
    p = Process(target=test_window)
    p.start()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants