Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<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>
 

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