Python 脚本卡在导入时
我有几个每分钟使用 crontab 运行的 python 脚本.使用 pip3 安装 python-binance 后,只是有时(对我来说似乎是随机的)我所有使用加密库的脚本都在导入时卡住了.
I have several python scripts which run using crontab every minute. After installing python-binance using pip3, only sometimes (seems random to me) all my scripts that use cryptography library get stuck at import.
正如您从下面的日志中看到的,本应每分钟运行一次且持续时间不到一秒的内容,有时却花费了十多分钟.
As you can see from the log below, what was supposed to run every minute and last less than a second, at some point took more than ten minutes.
2019-03-30 08:51:07 INFO (done)
2019-03-30 08:52:07 INFO (done)
2019-03-30 08:53:07 INFO (done)
2019-03-30 09:04:45 INFO (done)
2019-03-30 09:05:45 INFO (done)
2019-03-30 09:06:45 INFO (done)
为了调试问题,我使用 python -vv 循环运行脚本.
Trying to debug the problem, i run the script in a loop using python -vv.
在缓慢的迭代中,python解释器在这里卡了几分钟
During the slow iteration, the python interpreter was stuck here for several minutes
# trying /home/user/.local/lib/python3.6/site-packages/cryptography/hazmat/primitives/kdf/scrypt.cpython-36m-x86_64-linux-gnu.so
# trying /home/user/.local/lib/python3.6/site-packages/cryptography/hazmat/primitives/kdf/scrypt.abi3.so
# trying /home/user/.local/lib/python3.6/site-packages/cryptography/hazmat/primitives/kdf/scrypt.so
# trying /home/user/.local/lib/python3.6/site-packages/cryptography/hazmat/primitives/kdf/scrypt.py
# /home/user/.local/lib/python3.6/site-packages/cryptography/hazmat/primitives/kdf/__pycache__/scrypt.cpython-36.pyc matches /home/user/.local/lib/python3.6/site-packages/cryptography/hazmat/primitives/kdf/scrypt.py
# code object from '/home/user/.local/lib/python3.6/site-packages/cryptography/hazmat/primitives/kdf/__pycache__/scrypt.cpython-36.pyc'
import 'cryptography.hazmat.primitives.kdf.scrypt' # <_frozen_importlib_external.SourceFileLoader object at 0x7f5aff05d6a0>
附加信息:
- 操作系统:Ubuntu 18.04
- Python 版本:python3 3.6.7-1~18.04
Python-binance 版本:python-binance==0.7.1
- OS: Ubuntu 18.04
- Python version: python3 3.6.7-1~18.04
Python-binance version: python-binance==0.7.1
这不是资源问题,当解释器卡住时,CPU 使用率不到 20%,有大量可用 RAM 且没有磁盘瓶颈
It's not a resource issue, when the interpreter was stuck the CPU was less then 20% used, plenty of RAM free and no disk bottleneck
更新:在对 python discord 频道提出建议后,我又尝试了另一件事,但不幸的是没有帮助
UPDATE: After a suggestion on the python discord channel i have tried one more thing, which unfortunately didn't help
- 将 ulimit -n 从 1024 增加到 4096
非常感谢任何帮助
所以经过长时间的故障排除,在一些朋友的支持下,我发现问题与轮子有关.
So after a long long troubleshooting, with some friend's support i figured out the problem was related to wheels.
我已经使用以下方法在 venv 中安装了模块:
i have installed the module in a venv using:
pip3 install binance --no-binary :all:
--no-binary :all: 使 pip 从源代码编译依赖项,而不是使用任何可能可用的预编译轮.
--no-binary :all: is making pip compile dependencies from source code instead of using any pre-compiled wheels potentially available.
希望有人会发现此解决方案有帮助.
Hope someone will find this solution helpful.