HasteBin0

simple_pipe_test_pipe_exchange

Sep 12th, 2020
1,039
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python3
  2. from collections import deque
  3. from itertools import count
  4. from typing import Deque, List
  5.  
  6. from simple_pipe import *
  7.  
  8.  
  9. class SillyDemoPrimeTest:
  10.     _primes: Deque
  11.     _p_slice: List[int]
  12.     _counter: Iterable[int]
  13.  
  14.     def __init__(self):
  15.         self._primes = deque((2, 3, 5, 7))  # head start
  16.         self._p_slice = [0] * 20
  17.         self._counter = count(8)
  18.  
  19.     def print_next20(self) -> str:
  20.         ps = self._primes
  21.         p_slice = self._p_slice
  22.         counter = self._counter
  23.         for i in range(20):
  24.             for n in counter:
  25.                 if 0 not in (n % j for j in ps):
  26.                     ps.append(n)
  27.                     p_slice[i] = n
  28.                     break
  29.         return str(p_slice)
  30.  
  31.  
  32. ppc1 = ProcessPairController()
  33. ppc2 = ProcessPairController()
  34. send, receive = Pipe(True)
  35. lock_send_receive = Lock()
  36. done_send_pt1 = Event()
  37. done_send_pt2 = Event()
  38. done_receive_pt1 = Event()
  39. done_receive_pt2 = Event()
  40.  
  41.  
  42. def prime_test(sender: bool):
  43.     test = SillyDemoPrimeTest()
  44.     wait_progress_marker = (GeneratePPCExchange.send_sync if sender else GeneratePPCExchange.receive_sync)(ppc1, ppc2)
  45.     pab = ('Process AAA' if sender else 'Process BBB')
  46.     dp1, dp2 = ((done_send_pt1, done_send_pt2) if sender else (done_receive_pt1, done_receive_pt2))
  47.     for i in range(100):
  48.         wait_progress_marker()
  49.         output = test.print_next20()
  50.         with lock_send_receive:
  51.             send.send(f'{pab} on iteration #{i}.\n')
  52.             dp1.set()
  53.         event_wait_clear(dp2)
  54.         wait_progress_marker()
  55.         with lock_send_receive:
  56.             send.send(f'{pab} fount {output}.\n')
  57.             dp1.set()
  58.         event_wait_clear(dp2)
  59.     with lock_send_receive:  # close
  60.         dp1.set()
  61.  
  62.  
  63. threadS = Process(target = prime_test, name = "AAA", args = (True,))
  64. threadR = Process(target = prime_test, name = "BBB", args = (False,))
  65. threadS.start()
  66. threadR.start()
  67.  
  68. out = StringIO()
  69. locks = cycle(((done_send_pt1, done_send_pt2), (done_receive_pt1, done_receive_pt2)))
  70. first_blank = True
  71. while True:
  72.     l1, l2 = next(locks)
  73.     event_wait_clear(l1)
  74.     with lock_send_receive:
  75.         if receive.poll() == 0:
  76.             if first_blank:
  77.                 first_blank = False
  78.                 continue
  79.             else:
  80.                 break
  81.         out.write(receive.recv())
  82.         l2.set()
  83.  
  84. threadS.join()
  85. threadR.join()
  86.  
  87. out.seek(0)
  88. print(out.read())
  89.  
RAW Paste Data