Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Given your (unusual!) requirements, your implementation seems correct. In particular,</p> <ul> <li>If you're only updating one attribute, the Python GIL should be sufficient. Single bytecode instructions are atomic.</li> <li>If you do anything more complex, add locking! It's basically harmless anyway - if you cared about performance or multicore scalability, you probably wouldn't be using Python!</li> <li>In particular, be aware that <code>self.thing</code> and <code>self.n</code> in this code are updated in a separate bytecode instructions. The GIL could be released/acquired between, so you can't get a consistent view of the two of them unless you add locking. If you're not going to do that, I'd suggest removing <code>self.n</code> as it's an "attractive nuisance" (easily misused) or at least adding a comment/docstring with this caveat.</li> <li>Consumers don't need to make a copy. You're not ever mutating a particular object pointed to by <code>self.thing</code> (and couldn't with string objects; they're immutable) and Python is garbage-collected, so as long as a consumer grabbed a reference to it, it can keep accessing it without worrying too much about what other threads are doing. The worst that could happen is your program using a lot of memory from several generations of <code>self.thing</code> being kept alive.</li> </ul> <p>I'm a bit curious where your requirements came from. In particular, that you don't care if a <code>thing</code> is never used or used many times.</p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. COThanks, the requirements came from a need to change the performance of some code which is currently using a pull model on `producer_proc` to a push model, without appearing to change the interface of the code. The `thing` in my case is a raw video frame (note 6220800 == 1920*1080*3). The old code spawns a capture process any time a video frame is requested, which has some delay. I would rather the capture process is chugging away in the background all the time, to avoid this delay.
      singulars
    2. COBy the way, I have now a related problem which I unfortunately did not make clear in the requirements of original question - I need to read 25 things, i.e. almost 150 megabytes, per second. The code above works, but if there is heavy load on the CPU due to other stuff running in the OS, the producer thread can not run fast enough and the output gets blocked up. How can I ensure that I'm reading data as fast as it is produced, or alternatively, that data will be simply be dropped (at frame boundaries!) rather than trying to read every byte of output?
      singulars
    3. COHmm. It sounds like you have a pipe or character device to get the video frames? There's no seek then, so I don't believe there is any way to skip bytes. I don't see a way with the Python API to read() into a preallocated discard buffer either. As far as reading faster, my best suggestion would be to not use Python. :( (If not rewrite the whole program, then implement the producer as a C Python extension module.) Python's a fun language but terribly slow and with the Global Interpreter Lock, almost single-processor.
      singulars
 

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