1

Here's the code I have created to load animations in background using pyglet.resource.animation() function, while the app does some other things.

import pyglet
import threading

animations = dict()

class LoadingThread(object):
    def __init__(self):
        thread = threading.Thread(target=self.run, args=())
        thread.start()                                  # Start the execution

    def run(self):
        """ Method that runs forever """
        loadAnimations()
        print("Loaded all animations.")


def loadAnimations():
    global animations
    print("In loadAnimations")
    for animation in os.listdir(os.getcwd()):
        if animation.endswith(".gif"):
            print(animation)
            #Gives segmentation fault here
            animations[animation] = pyglet.resource.animation(animation)
    print("Loaded animations")

thread = LoadingThread()

Runs well when called normally without a thread. If there is any other way to deal with loading animations in background in pyglet, please suggest.

Thanks.

  • Might be a GIL-related issue related to this https://stackoverflow.com/q/16947842/8887313 – ATOMP Mar 08 '20 at 15:53
  • Thanks. Saw the answer, but still don't know how to use it as I am using pyglet for the animation and not Gtk. – Jaydip Bari Mar 08 '20 at 16:48
  • Don't know much about pyglet specifically, sorry. – ATOMP Mar 08 '20 at 17:30
  • So does the fault give you any indication of what object caused the issue? As a general statement, while python dictionaries are "thread safe", they aren't "thread aware". So if for instance, you test that an item is present in the dictionary and then use the item you tested, it might no longer be in the dictionary. You would need to have your own lock to make sure the two accesses to the object were done without the dictionary changing in between. – Frank Merrow Mar 08 '20 at 20:30
  • The segmentation fault occurs on line ``` animations[animation] = pyglet.resource.animation(animation) ``` As I am not using the dictionary until it is filled with all the animations, no other part of the program is using it. – Jaydip Bari Mar 09 '20 at 04:06
  • If you know for sure that animations isn't being used in any other thread, then I am guessing your issues would be found in pyglet . . . something in there doesn't like threading. You could prove this by receiving that call into a temp variable and then moving the temp variable into animations. – Frank Merrow Mar 09 '20 at 19:18
  • @JaydipBari So I googled pyglet and it didn't take me long to find several articles on multi-threading and pyglet. My quick reading would seem to indicate that you cannot call pyglet from anyplace but the main thread. The solution offered in one of those articles was to post a message back to the main thread so it could make the call. I'll leave it to you to research this in greater depth. – Frank Merrow Mar 09 '20 at 22:53

1 Answers1

0

As suggested by @Frank Merrow. The problem here was that I was using the function pyglet.resource.animation("filename.gif") function in my main thread too. So it was creating a segmentation fault. I found another function that can also load animation.

pyglet.image.load_animation("filename.gif")

Using this solved my problem.

Also this problem can be solved by starting two threads synchronously running main flow and the background work.