Python thread safe mutable object copy

Is python copy module stream?

If this is not the case, how do I copy \ transcoded objects \ copycopy in thread safe mode in python?

+4
source share
1 answer

The Python GIL protects bytecodes, not Python instructions (see short or long explanations). Since python implements both copy.copy() and copy.deepcopy() , they are certainly more than one bytecode, so no, they are not thread safe!

If you have to work with multiple threads, and there are many cases that you need, for example, for dedicated IO threads, do what needs to be done - use threading.Lock() . Please note that you can use an elegant element with an expression with a lock object .

+7
source

Source: https://habr.com/ru/post/1488313/


All Articles