Note that there are some explanatory texts on larger screens.

plurals
  1. POIs this an insane implementation of producer consumer type thing?
    primarykey
    data
    text
    <pre><code># file1.py class _Producer(self): def __init__(self): self.chunksize = 6220800 with open('/dev/zero') as f: self.thing = f.read(self.chunksize) self.n = 0 self.start() def start(self): import subprocess import threading def produce(): self._proc = subprocess.Popen(['producer_proc'], stdout=subprocess.PIPE) while True: self.thing = self._proc.stdout.read(self.chunksize) if len(self.thing) != self.chunksize: msg = 'Expected {0} bytes. Read {1} bytes'.format(self.chunksize, len(self.thing)) raise Exception(msg) self.n += 1 t = threading.Thread(target=produce) t.daemon = True t.start() self._thread = t def stop(self): if self._thread.is_alive(): self._proc.terminate() self._thread.join(1) producer = _Producer() producer.start() </code></pre> <p>I have written some code more or less like the above design, and now I want to be able to consume the output of <code>producer_proc</code> in other files by going: </p> <pre><code># some_other_file.py import file1 my_thing = file1.producer.thing </code></pre> <p>Multiple other consumers might be grabbing a reference to <code>file.producer.thing</code>, they all need to use from the same <code>producer_proc</code>. And the <code>producer_proc</code> should never be blocked. Is this a sane implementation? Does the python GIL make it thread safe, or do I need to reimplement using a Queue for getting data of the worker thread? Do consumers need to explicitly make a copy of the thing?</p> <p>I guess am trying to implement something like Producer/Consumer pattern or Observer pattern, but I'm not really clear on all the technical details of design patterns.</p> <ul> <li>A single producer is constantly making things</li> <li>Multiple consumers using things at arbitrary times</li> <li><code>producer.thing</code> should be replaced by a fresh thing as soon as the new one is available, most things will go unused but that's ok</li> <li>It's OK for multiple consumers to read the same thing, or to read the same thing twice in succession. They only want to be sure they have got the most recent thing when asked for it, not some stale old thing. </li> <li>A consumer should be able to keep using a thing as long as they have it in scope, even though the producer may have already overwritten his <code>self.thing</code> with a fresh new thing. </li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload