I have a loop where in each iteration I (re)calculate a long vector of values:
try:
while(1):
long_vec = recalculate( long_vec )
last_vec = long_vec.copy()
except KeyboardInterrupt:
print last_vec
long_vec and last_vec are numpy arrays.
My question is: can I be sure that when I press CTRL+C , it won’t break copy() function, and last_vec is not broken?
It’s not the
.copy()that matters, it’s the subsequent assignment of the reference (last_vec =) that does.I believe the assignment is atomic in CPython (i.e. it requires a single bytecode instruction).
In other words,
last_veccan either contain the value from the previous iteration, or the copied value from the current iteration. Pressing Ctrl-C cannot result inlast_veccontaining a partial copy of the new value.