问题描述
我们在网络驱动器上有一个 python 脚本 + 相关模块.我们同时从多台计算机同时运行此脚本.
We have a python script + associated modules on a network drive. We are running this script simultaneously from several computers at once.
有时其中一个随机失败并显示如下错误:
Sometimes one of them randomly fails with an error like this:
Traceback (most recent call last):
File "\\chifs02.int.tt.local\group\Development\Server Products\Automation\repos\mksutils\fetch.py", line 16, in <module>
from scripts import write_set_environment
NameError: Can't find file for module scripts
(filename \\chifs02.int.tt.local\group\Development\Server Products\Automation\repos\mksutils\scripts.pyc)
我注意到的一个奇怪之处是 .py 文件是几个月前最后一次修改的,但 .pyc 只存在了几个小时,尽管从那时起至少每天都在运行.
One oddity that I noticed was that the .py file was last modified months ago, but the .pyc is only hours old, despite being run at least daily since then.
我以前从未见过这个 NameError: Can't find file for module scripts
错误,而且 Google 也没有多大帮助.
I have never seen this NameError: Can't find file for module scripts
error before, and Google isn't being much help.
所有涉及的计算机都运行 Windows.
All computers involved are running Windows.
推荐答案
不要那样做,那个"是指有多台计算机尝试使用相同的 .pyc(s).
Don't do that, where "that" is have multiple computers trying to use the same .pyc(s).
pyc 被修改的可能原因是您在不同计算机上的 Python 版本可能略有不同.一个编写它的 .pyc 版本,另一个出现并发现它不是它所期望的,并编写了它自己的版本.
The likely reason the pyc gets modified is that you likely have slightly different versions of Python on different computers. One writes its version of the .pyc, and another comes along and sees that it's not what it expected, and writes its own version.
当不同的计算机同时尝试处理相同的 .pyc 文件时,可能会出现奇怪的错误,这完全不会让我感到惊讶.这是对竞争条件的邀请,因为没有使用锁定机制.
It would surprise me not at all to learn that strange bugs are possible in the situation where different computers are simultaneously trying to deal with the same .pyc files. This is an invitation for race conditions, as there is no locking mechanism in use.
要么让每个系统在运行之前将脚本复制到本地,要么禁用 .pyc 生成(-B 命令行标志或设置 PYTHONDONTWRITEBYTECODE 环境变量).
Either have each system copy the script to a local place before running it, or disable .pyc generation (-B command line flag or set the PYTHONDONTWRITEBYTECODE environment variable).
这篇关于通过网络同时运行 python 脚本.这个可以吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!