11

I have written a python script to use gstreamer (pygst and gst modules) to calculate replaygain tags, and it was crashing inconsistently with various gobject errors. I found somewhere that you could fix this by putting the following boilerplate at the top of your script:

import gobject
gobject.threads_init()

I tried it, and it worked. Can anyone explain why these lines are necessary, and why pygst doesn't do this itself?

Ryan C. Thompson
  • 40,856
  • 28
  • 97
  • 159

1 Answers1

14

Because, you can use gobject in a non threading environment. This is not unusual. When you use gobject in a threading environment, you need to explicitly initialize by calling gobject.threads_init(). This will also ensure that the when "C" functions are called, the GIL is freed.

Also from the function document :

The threads_init() function initializes the use of Python threading in the gobject module. This function is different than the gtk.gdk.threads_init() function as that function also initializes the gdk threads.

Basically, you tell gobject module explicitly that you are going to use threading and initialize it accordingly.

Community
  • 1
  • 1
pyfunc
  • 65,343
  • 15
  • 148
  • 136
  • But it seems that the python gstreamer module tries to use threading no matter what, as evidenced by the crashes that occur when I don't call `threads_init()`. So if it always tries to use threading, shouldn't it always initialize it? – Ryan C. Thompson Sep 25 '10 at 18:00
  • 5
    Or to put it more bluntly, why doesn't pygst include this line of code, which is required for it to work without crashing? – Ryan C. Thompson Sep 28 '10 at 23:00
  • 2
    I'd say it's not pygst's job to do that but the application's. If every library/binding did that, it would get done again every time you load one. – XTL Feb 15 '12 at 09:29