test.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. from __future__ import print_function
  2. from avent import Avent, Abject, StopThread, handleMainEvents
  3. import threading, time, sys
  4. #
  5. # FIXME: Out of date!
  6. #
  7. class A1(Abject):
  8. def __init__(self):
  9. self.onWhat = Avent()
  10. self.onSomething = Avent()
  11. def dothings(self, sender, eargs):
  12. print("{}: doing things: {}".format(
  13. threading.currentThread().getName(),
  14. threading.currentThread() == self._thread))
  15. def dosomething(self, sender, eargs):
  16. print("{}: doing things: {}".format(
  17. threading.currentThread().getName(),
  18. threading.currentThread() == self._thread))
  19. def loop(self):
  20. print("{}: A1 looping".format(threading.currentThread().getName()))
  21. i = 0
  22. while True:
  23. if self.handleEvents(): break
  24. if i == 5:
  25. print("\n{}: (a1) on what => mainthing".format(
  26. threading.currentThread().getName()))
  27. self.onWhat.fire(self)
  28. if i == 8:
  29. print("\n{}: (a1) on something => (a3) do things".format(
  30. threading.currentThread().getName()))
  31. self.onSomething.fire(self)
  32. i += 1
  33. time.sleep(1)
  34. class A2(Abject):
  35. onThings = Avent()
  36. onOther = Avent()
  37. main_t = threading.currentThread()
  38. def mainthing(sender, eargs):
  39. global main_t
  40. print("{}: main thread: {}".format(
  41. threading.currentThread().getName(),
  42. threading.currentThread() == main_t))
  43. if __name__ == "__main__":
  44. # init
  45. a1 = A1()
  46. a2 = A2()
  47. a3 = A1()
  48. t1 = StopThread(target=a1.loop)
  49. t2 = StopThread(target=a3.loop)
  50. # connect events
  51. a2.onThings += a1.dothings
  52. a2.onOther += mainthing
  53. a1.onWhat += mainthing
  54. a1.onSomething += a3.dothings
  55. # start da threads
  56. t1.start()
  57. time.sleep(3)
  58. t2.start()
  59. time.sleep(1)
  60. print("---------")
  61. print("\n{}: on things => (a1) do things".format(
  62. threading.currentThread().getName()))
  63. a2.onThings.fire(a2)
  64. time.sleep(1)
  65. print("\n{}: on other => mainthing".format(
  66. threading.currentThread().getName()))
  67. a2.onOther.fire(a2)
  68. time.sleep(6)
  69. t2.stop()
  70. t1.stop()
  71. print("\n{}: main events should execute now..".format(
  72. threading.currentThread().getName()))
  73. # this blocks forever
  74. handleMainEvents()
  75. # this should never print
  76. print("\n----------\nend")