[pygtk] idle_add and source_remove combination can cause freeze.

Antoon Pardon Antoon.Pardon at rece.vub.ac.be
Fri Oct 14 20:20:17 WST 2005


In experimenting with threads I found that a combination of
gobject.source_remove and gobject.idle_add can freeze the
application.

The program below will freeze on my machine if it is running
long enough. It does seem very sensitive to the sleeptime
value. Increasing that to 0.01 makes it less likely for
the bug to manifest itself.

Is it a bug or am I doing something wrong?


-------------------------------------------------------------

import pygtk
pygtk.require('2.0')

import gtk
import gtk.gdk as gdk
import gobject as go

from threading import Thread, Lock

from random import Random, randint
from time import sleep

cb_id = None
cb_lk = Lock()

sleeptime = 0.001
population = 4

def Draw():
  global cb_id
  cb_lk.acquire()
  print "callback"
  if cb_id is not None:
    if randint(0,population) == 0:
      print "removing idle callback"
      go.source_remove(cb_id)
      print "removed idle callback"
      cb_id = None
  cb_lk.release()
  return True
# end Draw

def Counting(Id):
  global cb_id

  sleep(2)
  Time = 0
  PRG = Random()
  while 1:
    print Id, "loop start"
    sleep(sleeptime)
    OldTime = Time
    Time = OldTime +  PRG.randint(1,1000)
    while OldTime < Time:
      OldTime += 1
    # while
    cb_lk.acquire()
    if cb_id is None:
      print Id, "registering callback"
      cb_id = go.idle_add(Draw)
      print Id, "registered callback"
    cb_lk.release()
#end Counting

Worker = [ Thread(target=Counting, args = (i,)) for i in xrange(7) ]

gdk.threads_init()
for W in Worker:
  W.start()
# for

gdk.threads_enter()
gtk.main()
gdk.threads_leave()


More information about the pygtk mailing list