9

Is python's copy module thread safe?

If not, how should I copy\deepcopy mutable objects in a thread-safe manner in python?

1
  • isn't everything (more ore less) thread safe in python... that's why the GIL keeps locking up... Commented Jun 26, 2013 at 14:34

1 Answer 1

14

Python's GIL protects bytecodes, not Python statements (see short or long explanations). As both copy.copy() and copy.deepcopy() are implemented in python, they are certainly more than a single bytecode, so no, they are not thread safe!

If you must work with multiple threads, and there are many cases you should such as having IO dedicated threads, do what must be done - use threading.Lock(). Notice you can use the elegant with statement with the lock object.

Sign up to request clarification or add additional context in comments.

1 Comment

wrong answer. Python collections are thread-safe. They themselves don't go corrupt, but their data may.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.