SIP4 python bindings for TQt
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

semaphore.py 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #!/usr/bin/env python
  2. #
  3. # A port of the semaphore example from TQt.
  4. import sys
  5. # Check if thread support was enabled.
  6. try:
  7. from python_tqt.qt import TQThread
  8. except:
  9. print "Thread support not enabled"
  10. sys.exit(1)
  11. from python_tqt.qt import *
  12. # The semaphore instances.
  13. yellowSem = None
  14. greenSem = None
  15. class YellowThread(TQThread):
  16. def __init__(self,o):
  17. TQThread.__init__(self)
  18. self.receiver = o
  19. self.stopped = 0
  20. self.mutex = TQMutex()
  21. def run(self):
  22. global yellowSem, greenSem
  23. for i in range(20):
  24. yellowSem += 1
  25. event = TQCustomEvent(12345)
  26. event.setData(TQString("Yellow!"))
  27. TQThread.postEvent(self.receiver,event)
  28. self.msleep(200);
  29. greenSem -= 1
  30. self.mutex.lock()
  31. if self.stopped:
  32. self.stopped = 0
  33. self.mutex.unlock()
  34. break
  35. self.mutex.unlock()
  36. yellowSem += 1
  37. event = TQCustomEvent(12346)
  38. event.setData(TQString("Yellow!"))
  39. TQThread.postEvent(self.receiver,event)
  40. greenSem -= 1
  41. def stop(self):
  42. self.mutex.lock()
  43. self.stopped = 1
  44. self.mutex.unlock()
  45. class GreenThread(TQThread):
  46. def __init__(self,o):
  47. TQThread.__init__(self)
  48. self.receiver = o
  49. self.stopped = 0
  50. self.mutex = TQMutex()
  51. def run(self):
  52. global yellowSem, greenSem
  53. for i in range(20):
  54. greenSem += 1
  55. event = TQCustomEvent(12345)
  56. event.setData(TQString("Green!"))
  57. TQThread.postEvent(self.receiver,event)
  58. self.msleep(200)
  59. yellowSem -= 1
  60. self.mutex.lock()
  61. if self.stopped:
  62. self.stopped = 0
  63. self.mutex.unlock()
  64. break
  65. self.mutex.unlock()
  66. greenSem += 1
  67. event = TQCustomEvent(12346)
  68. event.setData(TQString("Green!"))
  69. TQThread.postEvent(self.receiver,event)
  70. self.msleep(10)
  71. yellowSem -= 1
  72. def stop(self):
  73. self.mutex.lock()
  74. self.stopped = 1
  75. self.mutex.unlock()
  76. class SemaphoreExample(TQWidget):
  77. def __init__(self):
  78. TQWidget.__init__(self)
  79. self.yellowThread = YellowThread(self)
  80. self.greenThread = GreenThread(self)
  81. global yellowSem, greenSem
  82. yellowSem = TQSemaphore(1)
  83. greenSem = TQSemaphore(1)
  84. self.button = TQPushButton("&Ignition!",self)
  85. self.connect(self.button,SIGNAL("clicked()"),self.startExample)
  86. self.mlineedit = TQMultiLineEdit(self)
  87. self.label = TQLabel(self)
  88. vbox = TQVBoxLayout(self,5)
  89. vbox.addWidget(self.button)
  90. vbox.addWidget(self.mlineedit)
  91. vbox.addWidget(self.label)
  92. def __del__(self):
  93. stopYellow = self.yellowThread.running()
  94. stopGreen = self.greenThread.running()
  95. if stopYellow:
  96. self.yellowThread.stop()
  97. if self.greenThread.running():
  98. self.greenThread.stop()
  99. if stopYellow:
  100. self.yellowThread.wait()
  101. if stopGreen:
  102. self.greenThread.wait()
  103. global yellowSem, greenSem
  104. yellowSem = None
  105. greenSem = None
  106. def startExample(self):
  107. if self.yellowThread.running() or self.greenThread.running():
  108. TQMessageBox.information(self,"Sorry",
  109. "The threads have not completed yet, and must finish before "
  110. "they can be started again.")
  111. return
  112. self.mlineedit.clear()
  113. global yellowSem
  114. while yellowSem.available() < yellowSem.total():
  115. yellowSem -= 1
  116. yellowSem += 1
  117. self.yellowThread.start()
  118. self.greenThread.start()
  119. def customEvent(self,event):
  120. if event.type() == 12345:
  121. s = event.data()
  122. self.mlineedit.append(s)
  123. if s.latin1() == "Green!":
  124. self.label.setBackgroundColor(TQt.green)
  125. else:
  126. self.label.setBackgroundColor(TQt.yellow)
  127. self.label.setText(s)
  128. del s
  129. elif event.type() == 12346:
  130. s = event.data()
  131. TQMessageBox.information(self,s.latin1() + " - Finished",
  132. "The thread creating the \"" + s.latin1() +
  133. "\" events has finished.")
  134. del s
  135. else:
  136. print "Unknown custom event type:", event.type()
  137. app = TQApplication(sys.argv)
  138. se = SemaphoreExample()
  139. app.setMainWidget(se)
  140. se.show()
  141. sys.exit(app.exec_loop())