python thread safe mutable object copy

血红的双手。 提交于 2019-12-23 10:07:02

问题


Is python's copy module thread safe?

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


回答1:


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.



来源:https://stackoverflow.com/questions/17322726/python-thread-safe-mutable-object-copy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!