Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. import threading
  2. import queue
  3. import time
  4.  
  5. class Program:
  6. def __init__(self):
  7. self.queue = queue.Queue()
  8.  
  9. self.pipeline = Pipeline(self)
  10. self.web = Webserver(self)
  11.  
  12. def mainloop(self):
  13. while True:
  14. task = self.queue.get()
  15. task.run()
  16.  
  17. class FifoLock:
  18. def __init__(self, program):
  19. self.program = program
  20. self.queue = self.program.queue
  21. self.condition = threading.Condition()
  22.  
  23. # blocks until run() is called
  24. def __enter__(self):
  25. # equivalent to .acquire() ?
  26. self.condition.__enter__()
  27.  
  28. self.queue.put(self)
  29. self.condition.wait()
  30.  
  31. def __exit__(self, *exc_info):
  32. self.condition.notify_all()
  33.  
  34. # equivalent to .release() ?
  35. self.condition.__exit__(*exc_info)
  36.  
  37. # releases self from __enter__()
  38. # blocks until __exit__()
  39. def run(self):
  40. with self.condition:
  41. self.condition.notify_all()
  42. self.condition.wait()s
  43.  
  44. class Pipeline:
  45. def __init__(self, program):
  46. self.program = program
  47. self.lock = FifoLock(self.program)
  48.  
  49. def run(self):
  50. print("schedule p")
  51. with self.lock:
  52. print("pipeline")
  53. for x in range(3):
  54. print("p", x)
  55. time.sleep(.5)
  56. print("p exit")
  57.  
  58. def mainloop(self):
  59. while True:
  60. self.run()
  61.  
  62. class Webserver:
  63. def __init__(self, program):
  64. self.program = program
  65. self.lock = FifoLock(self.program)
  66.  
  67. def run(self):
  68. print("schedule w")
  69. with self.lock:
  70. print("web")
  71. for x in range(3):
  72. print("w", x)
  73. time.sleep(.51234)
  74. print("w exit")
  75.  
  76. p = Program()
  77. threading.Thread(target=p.mainloop).start()
  78. time.sleep(1)
  79. threading.Thread(target=p.pipeline.mainloop).start()
  80. time.sleep(5.34)
  81. threading.Thread(target=p.web.run).start()
  82. time.sleep(3.789)
  83. threading.Thread(target=p.web.run).start()
  84. time.sleep(5)
  85. import os
  86. os.abort()
  87.  
  88.  
  89. # Below is a runthrough
  90.  
  91. $ python FifoLock.py
  92. schedule p
  93. pipeline
  94. p 0
  95. p 1
  96. p 2
  97. p exit
  98. schedule p
  99. pipeline
  100. p 0
  101. p 1
  102. p 2
  103. p exit
  104. schedule p
  105. pipeline
  106. p 0
  107. p 1
  108. p 2
  109. p exit
  110. schedule p
  111. pipeline
  112. p 0
  113. p 1
  114. schedule w
  115. p 2
  116. p exit
  117. schedule p
  118. web
  119. w 0
  120. w 1
  121. w 2
  122. w exit
  123. pipeline
  124. p 0
  125. p 1
  126. p 2
  127. p exit
  128. schedule p
  129. pipeline
  130. p 0
  131. schedule w
  132. p 1
  133. p 2
  134. p exit
  135. schedule p
  136. web
  137. w 0
  138. w 1
  139. w 2
  140. w exit
  141. pipeline
  142. p 0
  143. p 1
  144. p 2
  145. p exit
  146. schedule p
  147. pipeline
  148. p 0
  149. p 1
  150. Aborted (core dumped)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement