python - Why does Python ignore source files in favor of pyc files sometimes?
问题描述
I've got a situation where I rapidly change Python source files on disk and, for each change, run them with Python in a subprocess (it's for a mutation testing tool). I have found that in some cases, the Python subprocess either doesn't see the change or ignores it in favor the contents of __pycache__
. Here's an example:
from pathlib import Path
import subprocess
workspace = Path('workspace')
workspace.mkdir(exist_ok=True)
for i in range(3):
with open(workspace / 'code.py', mode='wt') as f:
f.write(f"print({i})")
proc = subprocess.run(
'python -m code'.split(),
stdout=subprocess.PIPE,
cwd='workspace')
print(i, proc.stdout.decode('utf-8').strip())
I would expect this to print:
0 0
1 1
2 2
But generally it prints something like:
0 0
1 0
2 0
or
0 0
1 1
2 1
That is, at some point the changes I'm writing to disk aren't seen by the Python subprocess.
I've tried all sorts of attempts to flush the changes to disk with os.fsync
, flush()
on the file handle, and so forth. The only thing that seems to make a difference is putting a substantial time.sleep()
after the closing of the file handle.
I could tell Python to not use pycs by setting PYTHONDONTWRITEBYTECODE
, but at this point I'd like to know what's going on.
So, am I right that this is just some failure to flush to disk? Or is there perhaps a bug/feature of Python that I'm fighting?
解决方案
No special flushing is required, but aside from disabling or removing the .pyc
, sleep is the only answer: the .pyc
doesn’t appear older than the .py
if the time elapsed between writing them is small enough. “Small enough” depends on the filesystem and OS, but it could easily be a whole second.
推荐阅读
- javascript - JavaScript 的街道地址正则表达式
- graphql - AWS Amplify 基于 db 字段值或关系的授权
- sql - sql:查询以查找带有额外列的最大计数
- python - 通过索引查找和替换 XML 属性 - Python
- python - 有没有办法将 Python 转换为 R?
- ruby-on-rails - Rails 6 无法部署到 Heroku:ModuleNotFoundError:找不到模块:错误:无法解析 '/tmp 中的 '@popperjs/core'
- postman - 从邮递员的外部json数据文件中读取值
- node.js - 如何使用 Node.js SDK 创建 Kubernetes 部署
- jsf - 如何防止 JSF 2.2 接受来自不同会话的 ViewState?
- amazon-web-services - 如何正确使用 AWS Shield Standard?