Python pyc possible when power cycling?

被刻印的时光 ゝ 提交于 2019-12-12 17:30:45

问题


Wondering if python is able to institute checks within their compile-phase of a given py to pyc to guard against a corrupt pyc due to sudden power-down of the system(and disk). When the system comes back up will the pyc that may exist be checked for integrity and if its considered suspect, regenerated?


回答1:


As far as I know, the compile-to-bytecode process works the same as normal make in how it handles recompilation; it checks if the compiled files are older than the source files, and if they are it recompiles while if they aren't it leaves them be. My best suggestion would be to just clear the PYC files whenever sudden power failure is suspected and do an import to get the new ones (you could probably automate this, too, to make it simpler).

Note that I have not experimented to see if Python waits until a bytecode file is complete before writing to disk; if it does, that would reduce the possibility of the sort of corruption you talk about, as the PYC file would simply not be written to disk to begin with if the power failure occurred during actual compilation. However, corruption would still be an issue if the power loss occurs during the write, as I believe most OSes update a file's modification time when the file is opened with write access rather than when the file handle is closed.




回答2:


If I interpret this message correctly, Python 3.3 will create .pyc file under a different name and rename it on successful completion. I think that's as "atomic" (their term) as it gets in this context, and it will certainly protect you from a sudden crash or loss of power. Earlier versions (including the 2.* branch) are apparently still subject to race conditions and invalid .pyc files.

Whether a file could be invalid without triggering a runtime exception is a separate question, though. The problem reports all talk about failing on import.



来源:https://stackoverflow.com/questions/17028568/python-pyc-possible-when-power-cycling

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