|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import concurrent.futures |
| 3 | +import logging |
| 4 | +import random |
| 5 | +import threading |
| 6 | +import time |
| 7 | + |
| 8 | + |
| 9 | +class Pipeline: |
| 10 | + """Class to allow a single element pipeline between producer and consumer. |
| 11 | + """ |
| 12 | + |
| 13 | + def __init__(self): |
| 14 | + self.value = 0 |
| 15 | + self._set_lock = threading.Lock() |
| 16 | + self._get_lock = threading.Lock() |
| 17 | + self._get_lock.acquire() |
| 18 | + |
| 19 | + def get_value(self, name): |
| 20 | + logging.debug("%s:about to acquire getlock", name) |
| 21 | + self._get_lock.acquire() |
| 22 | + logging.debug("%s:have getlock", name) |
| 23 | + value = self.value |
| 24 | + logging.debug("%s:about to release setlock", name) |
| 25 | + self._set_lock.release() |
| 26 | + logging.debug("%s:setlock released", name) |
| 27 | + return value |
| 28 | + |
| 29 | + def set_value(self, value, name): |
| 30 | + logging.debug("%s:about to acquire setlock", name) |
| 31 | + self._set_lock.acquire() |
| 32 | + logging.debug("%s:have setlock", name) |
| 33 | + self.value = value |
| 34 | + logging.debug("%s:about to release getlock", name) |
| 35 | + self._get_lock.release() |
| 36 | + logging.debug("%s:getlock released", name) |
| 37 | + |
| 38 | + |
| 39 | +def producer(pipeline, event): |
| 40 | + """Pretend we're getting a number from the network.""" |
| 41 | + while not event.is_set(): |
| 42 | + new_datapoint = random.randint(1, 101) |
| 43 | + # Sleep to simulate waiting for data from network |
| 44 | + # time.sleep(float(new_datapoint)/100) |
| 45 | + logging.info("Producer got data %d", new_datapoint) |
| 46 | + pipeline.set_value(new_datapoint, "Producer") |
| 47 | + if event.is_set(): |
| 48 | + logging.info("Producer received internal event. Exiting") |
| 49 | + |
| 50 | + # don't put sleep here as this will cause the system to stall when the |
| 51 | + # consumer is active. That will result in deadlock. |
| 52 | + # time.sleep(float(new_datapoint)/100) |
| 53 | + |
| 54 | + logging.info("Producer received event. Exiting") |
| 55 | + |
| 56 | + |
| 57 | +def consumer(pipeline, event): |
| 58 | + """ Pretend we're saving a number in the database. """ |
| 59 | + datapoint = 0 |
| 60 | + while not event.is_set(): |
| 61 | + datapoint = pipeline.get_value("Consumer") |
| 62 | + logging.info("Consumer storing data: %d", datapoint) |
| 63 | + |
| 64 | + logging.info("Consumer received event. Exiting") |
| 65 | + |
| 66 | + |
| 67 | +if __name__ == "__main__": |
| 68 | + format = "%(asctime)s: %(message)s" |
| 69 | + logging.basicConfig(format=format, level=logging.INFO, datefmt="%H:%M:%S") |
| 70 | + # logging.getLogger().setLevel(logging.DEBUG) |
| 71 | + |
| 72 | + pipeline = Pipeline() |
| 73 | + event = threading.Event() |
| 74 | + with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor: |
| 75 | + executor.submit(producer, pipeline, event) |
| 76 | + executor.submit(consumer, pipeline, event) |
| 77 | + |
| 78 | + time.sleep(0.1) |
| 79 | + logging.info("Main: about to set event") |
| 80 | + event.set() |
0 commit comments