Threads freeze

Post here if you need help with creating a Graphical User Interface in Python.

Threads freeze

Postby irtomek » Sat May 25, 2013 11:10 am

Hey I've got some issues with creating a plane of colorful fields. This is they freeze for some time after a moment of use.

What is more I find it difficult to terminate Threads after closing an application.

Code: Select all
#!/usr/bin/python

import sys
import threading
import time
import random
from PyQt4 import QtGui,QtCore
from multiprocessing import Process


class Field(QtGui.QWidget, threading.Thread):
   def __init__(self, x, y, X, Y, time, table, p):
      QtGui.QWidget.__init__(self)
      threading.Thread.__init__(self)
      self.x = x
      self.y = y
      self.X = X
      self.Y = Y
      self.time = time
      self.p = p
      self.setAutoFillBackground(True)

      self.directChange()
      self.table = table

   def directChange(self):
      self.r = random.randint(0,255)
      self.g = random.randint(0,255)
      self.b = random.randint(0,255)

      p = self.palette()
      p.setColor(self.backgroundRole(), QtGui.QColor(self.r,self.g,self.b))
      self.setPalette(p)

   def northRow(self):
      if 0<self.x:
         return self.x-1
      else: return self.X-1
   def southRow(self):
      if self.x < self.X-1:
         return self.x+1
      else: return 0
   def westCol(self):
      if 0 < self.y:
         return self.y-1
      else:
         return self.Y-1
   def eastCol(self):
      if self.y < self.Y-1:
         return self.y+1
      else: return 0

   def indirectChange(self, x, y, table):
      self.r = (table[self.northRow()][y].r + table[self.southRow()][y].r + \
            table[x][self.westCol()].r + table[x][self.eastCol()].r) /4

      self.g = (table[self.northRow()][y].g + table[self.southRow()][y].g + \
            table[x][self.westCol()].g + table[x][self.eastCol()].g) /4

      self.b = (table[self.northRow()][y].b + table[self.southRow()][y].b + \
            table[x][self.westCol()].b + table[x][self.eastCol()].b) /4

      p = self.palette()
      p.setColor(self.backgroundRole(), QtGui.QColor(self.r,self.g,self.b))
      self.setPalette(p)

   def run(self):
      threadLimiter.acquire()
      try:
         with threading.Lock():
            self.th =  threading.Timer(self.time,self.run).start()
            if(random.random()<self.p):
               self.directChange()
            else:
               self.indirectChange(self.x, self.y, self.table)
      finally: threadLimiter.release()


class DanceFloor(QtGui.QWidget):
   def __init__(self, i, j, time, p):
      self.i = i
      self.j = j
      QtGui.QWidget.__init__(self)
      self.resize(400,400)
      self.move(100,100)
      self.setWindowTitle("Dancefloor")

      self.pos = [[0 for k in range(j)] for l in range(i)]
      for k in range(i):
         for l in range(j):
            self.pos[k][l] = Field(k,l, i, j, time, self.pos, p)

      grid = QtGui.QGridLayout()
      self.setLayout(grid)
      
      for k in range(i):
         for l in range(j):
            grid.addWidget(self.pos[k][l],k,l)
      for k in range(i):
         for l in range(j):
            th = self.pos[k][l]
            th.start()
      self.show()

   def closeEvent(self, event):
      pass

app = QtGui.QApplication(sys.argv)
app.setQuitOnLastWindowClosed(True)
threadLimiter = threading.BoundedSemaphore(15)
frame = DanceFloor(7,7, 0.1, 0.1)

sys.exit(app.exec_())

if __name__ == '__main__':
   main()
irtomek
 
Posts: 2
Joined: Sat May 25, 2013 11:06 am

Re: Threads freeze

Postby irtomek » Sat May 25, 2013 11:22 am

But I found it is possible to UNFREEZE threads with resizing a window !
irtomek
 
Posts: 2
Joined: Sat May 25, 2013 11:06 am


Return to GUI

Who is online

Users browsing this forum: No registered users and 0 guests