微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

解决python ValueError: max_workers must be <= 61 when running pre-commit?

如何解决解决python ValueError: max_workers must be <= 61 when running pre-commit?

我正在使用 Django 开发 ERP,我想在我的项目中使用预提交。

我已经安装了 pre-commit、black、Flake8Flake8-black。 这是我的

.pre-commit-config.yaml

文件配置内容

repos:
-   repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.3.0
hooks:
-   id: check-yaml
-   id: end-of-file-fixer
-   id: trailing-whitespace
-   repo: https://github.com/psf/black
rev: 19.3b0
hooks:
-   id: black
    language_version: python3.7

当我提交代码时,它给了我这个错误

Check 
Yaml...............................................................Passed
Fix End of Files.........................................................Failed
- hook id: end-of-file-fixer
- exit code: 1
- files were modified by this hook

Fixing vms/movement/test/test_model.py
Fixing vms/payment/test/test_forms.py

Trim Trailing Whitespace.................................................Passed
black....................................................................Failed
- hook id: black
- exit code: 1

Traceback (most recent call last):
  File "C:\Program Files\python37\lib\runpy.py",line 193,in _run_module_as_main
    "__main__",mod_spec)
  File "C:\Program Files\python37\lib\runpy.py",line 85,in _run_code
    exec(code,run_globals)
  File "C:\Users\Diaa\.cache\pre-commit\repo4t4vqwkg\py_env-python3\Scripts\black.EXE\__main__.py",line 7,in <module>
  File "c:\users\diaa\.cache\pre-commit\repo4t4vqwkg\py_env-python3\lib\site-packages\black.py",line 3754,in patched_main
    main()
  File "c:\users\diaa\.cache\pre-commit\repo4t4vqwkg\py_env-python3\lib\site-packages\click\core.py",line 1137,in __call__
    return self.main(*args,**kwargs)
  File "c:\users\diaa\.cache\pre-commit\repo4t4vqwkg\py_env-python3\lib\site-packages\click\core.py",line 1062,in main
    rv = self.invoke(ctx)
  File "c:\users\diaa\.cache\pre-commit\repo4t4vqwkg\py_env-python3\lib\site-packages\click\core.py",line 1404,in invoke
    return ctx.invoke(self.callback,**ctx.params)
  File "c:\users\diaa\.cache\pre-commit\repo4t4vqwkg\py_env-python3\lib\site-packages\click\core.py",line 763,in invoke
    return __callback(*args,**kwargs)
  File "c:\users\diaa\.cache\pre-commit\repo4t4vqwkg\py_env-python3\lib\site-packages\click\decorators.py",line 26,in new_func
    return f(get_current_context(),*args,**kwargs)
  File "c:\users\diaa\.cache\pre-commit\repo4t4vqwkg\py_env-python3\lib\site-packages\black.py",line 435,in main
    executor = ProcesspoolExecutor(max_workers=os.cpu_count())
  File "C:\Program Files\python37\lib\concurrent\futures\process.py",line 523,in __init__
    f"max_workers must be <= {_MAX_WINDOWS_WORKERS}")
ValueError: max_workers must be <= 61

这是我的点子列表

-------------------------------- --------
appdirs                          1.4.4
asgiref                          3.3.4
aspy.refactor-imports            2.2.0
atomicwrites                     1.4.0
attrs                            21.2.0
Babel                            2.9.1
black                            21.6b0
cached-property                  1.5.2
cfgv                             3.3.0
click                            8.0.1
colorama                         0.4.4
coverage                         5.5
distlib                          0.3.2
Django                           3.2
django-autocomplete-light        3.8.2
django-autoslug                  1.9.8
django-colorfield                0.4.1
django-countries                 7.2.1
django-crispy-forms              1.11.0
django-datatables-view           1.19.1
django-excel                     0.0.10
django-guardian                  2.4.0
django-ipware                    3.0.2
django-js-asset                  1.2.2
django-login-required-middleware 0.5.0
django-mptt                      0.12.0
django-nested-inline             0.4.3
django-querysetsequence          0.14
django-rest-authtoken            2.1.3
django-timezone-field            4.1.1
djangorestframework              3.12.2
docopt                           0.6.2
enum34                           1.1.10
Faker                            8.8.0
filelock                         3.0.12
Flake8                           3.9.2
Flake8-black                     0.2.1
fpdf2                            2.4.1
identify                         2.2.10
importlib-Metadata               4.5.0
iniconfig                        1.1.1
IP2Location                      8.5.1
isort                            5.9.1
lml                              0.0.9
mccabe                           0.6.1
mixer                            7.1.2
mypy-extensions                  0.4.3
nodeenv                          1.6.0
num2words                        0.5.10
packaging                        20.9
pathspec                         0.8.1
Pillow                           8.1.0
pip                              21.1.1
pluggy                           0.13.1
pre-commit                       2.13.0
psycopg2                         2.8.6
py                               1.10.0
pycodestyle                      2.7.0
pycountry                        20.7.3
pydocstyle                       6.1.1
pyexcel                          0.6.2
pyexcel-io                       0.6.4
pyexcel-webio                    0.1.2
pyflakes                         2.3.1
pynput                           1.7.3
pyparsing                        2.4.7
pytest                           6.2.4
pytest-django                    4.4.0
python-dateutil                  2.8.1
pytz                             2021.1
PyYAML                           5.4.1
regex                            2021.4.4
reportbro-fpdf                   1.7.10
reportbro-lib                    1.6.0
reportlab                        3.5.67
seed-isort-config                2.2.0
setuptools                       57.0.0
setuptools-scm                   3.4.3
simpleeval                       0.9.10
six                              1.15.0
snowballstemmer                  2.1.0
sqlparse                         0.4.1
text-unidecode                   1.3
texttable                        0.8.2
toml                             0.10.2
tox                              3.23.1
typed-ast                        1.4.3
typing                           3.7.4.3
typing-extensions                3.10.0.0
virtualenv                       20.4.7
XlsxWriter                       1.4.3
xlwt                             1.3.0
zipp                             3.4.1

当我跑步时 os.cpu_count() 它给了我 64 个处理器。 我使用的是 Windows Server 2016 数据中心。

请帮我解决这个问题。

注意我使用的是 python 3.7

解决方法

这是 Windows 上 cpython 的一个已知问题。当 black 尝试在 > 60 个核心机器上运行多个工作程序时会发生错误,因为 os.cpu_count() 给出的默认进程工作程序数量打破了其他一些窗口限制(等待进程的数量?我不太确定)。 Black >=19.10b0 对此进行了修复,因此如果可以,请尝试在您的预提交配置中更新 black 版本?

,

您可以尝试修补此行site-packages\black.py,line 435

    executor = ProcessPoolExecutor(max_workers=os.cpu_count())

   executor = ProcessPoolExecutor(max_workers=min(32,os.cpu_count() + 4))

我相信 Python 3.8 及更高版本中的 ProcessPoolExecutor 本质上是这样的。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。