Python threads

Emmanuel Pietriga emmanuel.pietriga at xrce.xerox.com
Wed Dec 8 05:46:08 EST 1999


I seem to have a problem with pyhon's thred scheduler.

I have created an application using XPython, in which I want to have two
threads:
-one is in charge of repainting a drawingArea periodically (about every
20 ms)
-the other is in charge of listening for events


To test this, before doing something complex, I have simply tested a
little program in which I repaint some squares which are moved every
time the drawingArea is repainted. The eventListener thread just prints
the name of the event.

The problem is that, even with time.sleep(???) in the eventListener
thread, the other one (repainting thread) isn't allowed to execute very
often (once every 4 sec., and in a very heratic way).


I have also tried to put one of the thread's code in the while1: pass of
the main function. It behaves the same.
I think the problem comes from the thread scheduler. But I don't know
what to do!

Can anyone help me please?

Thanks.


I run python 1.5.2 with XPython  in a SOLARIS 2.6 environment.

Note that Xt.NextEvent() is a blocking function  (I mean, it returns
when an event occurs)

Here is the code of my little test program:

import sys
import Xt
import Xm
import X
import whrandom
import Xcursorfont
import thread
import time

global drawingList

def main():
 global drawingList
 toplevel = Xt.Initialize('toplevel',[],sys.argv)
 da = toplevel.CreateDrawingArea('da',{})
 da.SetValues({'background':'gray','width':800,'height':600})
 da.ManageChild()
 toplevel.RealizeWidget()
 pm=toplevel.CreatePixmap(800,600)
 gc=pm.CreateGC({'foreground':3})
 drawingList=[]
 for i in range(20):
  for j in range(20):
   drawingList.append(SGlyph(i*10,j*10))
 print 'trying to start threads'
 try:
  thread.start_new_thread(listenThr,())
  thread.start_new_thread(drawThr,tuple([da,pm,gc]))
  print 'threads started'
 except:
  print 'Couldn\'t start thread'
 while 1:
  pass



def drawThr(da,pm,gc):
 offset=0
 while 1:
  offset=offset+1
  gc.SetForeground(2)
  gc.FillRectangle(0,0,800,600)
  for o in drawingList:
   o.drawMe(gc,10+offset)
  pm.CopyArea(da,gc,0,0,800,600,0,0)
  time.sleep(0.2)


def listenThr():
 while 1:
  evt=Xt.NextEvent()
  #print evt.__name__
  time.sleep(0.01)


class SGlyph:
 def __init__(self,xc,yc):
  self.x=xc
  self.y=yc
 def drawMe(self,gc,offset):
  gc.SetForeground(5)
  gc.FillRectangle(self.x+offset-2,self.y+offset-2,4,4)

main()





More information about the Python-list mailing list