Elpy: Python 100% CPU usage

Created on 21 Jan 2017  Â·  10Comments  Â·  Source: jorgenschaefer/elpy

Hi, I am using Elpy everyday in my work and it is very solid package, congrats!

I am experiencing two major problems only. One with flake8 and another one with freezing and hanging problems. I could not yet get the right error message for flake8, but I will try to reproduce.

However, the freeze problems is constant. Every once in a while my emacs freezes and I can't type anything. I see the process list and it is showing Python 100% CPU.

I used M-x profiler-start to see what was consuming so much processing time. The top 1 was this:

redisplay_internal (C function)

Sorry if I can't inform much, how can I contribute to give more info on this issue? Is something already reported? (couldn't find on my own)

Thanks!

Bug

Most helpful comment

Same symptoms here.

TL,DR: endless loop in jedi 0.10.0

After some digging, I believe 100% CPU usage while editing is related to https://github.com/davidhalter/jedi/issues/878. I have (cumbersomely) upgaded to the dev branch of jedi with the patch applied and the issue seems to be gone :).

Note: if project is large or you have Lots of package installed, it will take some time and 100% for jedi to go through all the py files, but that is normal.

All 10 comments

Hello, and thanks for the report!
How large is the Python file you are editing?
Can you try M-x toggle-debug-on-quit and hit C-g when it freezes? That should bring up a backtrace you can post here.

Hi @jorgenschaefer ,
Indeed thanks for this great package!

I'm experiencing exacly the same issue as stated above:

However, the freeze problems is constant. Every once in a while my emacs freezes and I can't type anything. I see the process list and it is showing Python 100% CPU.

As you proposed I tried to enable the debug mode and you can find the backtrace I got in the attached file. Do you have any idea how to resolve this issue?

Thanks!

debug_elpy.txt

@jorgenschaefer I am working in files with hundreds of lines (max).. something like 10 ~ 20 buffers opened.

I am also using the Rope package as the elpy backend. All my settings related to Elpy are the following:

(elpy-enable)
(remove-hook 'elpy-modules 'elpy-module-highlight-indentation)
(remove-hook 'elpy-modules 'elpy-module-django)

(setq elpy-rpc-timeout 3)
(setq python-indent-offset 4)
(setq elpy-rpc-backend "jedi")
(setq elpy-rpc-python-command "python")
(setq python-shell-interpreter "python")
(setq python-shell-interpreter-args "")
(setq elpy-test-discover-runner-command '("python" "-m" "pytest"))

(setq python-shell-prompt-detect-failure-warning nil
      python-shell-completion-native nil
      python-shell-completion-native-disabled-interpreters '("pypy" "manage.py"))

(add-hook 'python-mode-hook
          (lambda ()
            (setq-default indent-tabs-mode nil)
            (setq-default tab-width 4)
            (setq-default python-indent 4)))

(setq python-shell-completion-native-enable nil)
(setq shell-file-name "/bin/bash")

@jjlk The backtrace indicates that Emacs is hanging while trying to write to the Python background process. Which happens when the Python backend is busy.

If possible, please use strace to check what the backend is doing. The backend can be busy if you have a large project root. It's difficult to guess the cause remotely, I'm afraid.

Well I have a small project (just one file) and it also hangs at 100% CPU usage with freezing emacs. I can't reproduce it and for me it seem to happend randomly. System data:

ArchLinux: 4.9.8-1-ARCH
emacs 25.1-4
python 3.6.0-2
ipython 5.1.0-2

Greetings

Same symptoms here.

TL,DR: endless loop in jedi 0.10.0

After some digging, I believe 100% CPU usage while editing is related to https://github.com/davidhalter/jedi/issues/878. I have (cumbersomely) upgaded to the dev branch of jedi with the patch applied and the issue seems to be gone :).

Note: if project is large or you have Lots of package installed, it will take some time and 100% for jedi to go through all the py files, but that is normal.

Nice catch, thank you!

Seems to be fixed

I'm having this issue with the following setup:

Elpy Configuration

Emacs.............: 28.0.50
Elpy..............: 1.32.0
Virtualenv........: py38 (/home/arien/anaconda3/envs/py38)
Interactive Python: python 3.8.1 (/home/arien/anaconda3/envs/py38/bin/python)
RPC virtualenv....: py38 (/home/arien/anaconda3/envs/py38)
 Python...........: python 3.8.1 (/home/arien/anaconda3/envs/py38/bin/python)
 Jedi.............: 0.16.0
 Rope.............: Not found (0.16.0 available)
 Autopep8.........: 1.5
 Yapf.............: Not found (0.29.0 available)
 Black............: 19.10b0
Syntax checker....: pychecker.sh (/home/arien/bin/pychecker.sh)

package-list-packges shows

Package elpy is installed.

     Status: Installed in ‘elpy-20200202.2031/’ (unsigned). Delete
    Version: 20200202.2031
    Summary: Emacs Python Development Environment
   Requires: company-0.9.2, emacs-24.4, highlight-indentation-0.5.0, pyvenv-1.3, yasnippet-0.8.0,
               s-1.11.0
Other versions: 20200202.2031 (melpa).

This is the CPU profile report output:

- command-execute                                                 272  70%
 - call-interactively                                             272  70%
  - funcall-interactively                                         203  52%
   - execute-extended-command                                     181  46%
    - execute-extended-command--shorter                           168  43%
     - completion-try-completion                                  168  43%
      - completion--nth-completion                                168  43%
       - completion--some                                         168  43%
        + #<compiled 0x8081f14d3d32f28>                           168  43%
    - sit-for                                                      12   3%
     - redisplay                                                   11   2%
      - redisplay_internal (C function)                             3   0%
       - js2-mode-idle-reparse-inner                                2   0%
        - js2-reparse                                               2   0%
         - js2-parse                                                2   0%
          - js2-do-parse                                            2   0%
           - js2-get-token                                          1   0%
              js2-get-token-internal                                1   0%
           - js2-parse-statement                                    1   0%
            - js2-statement-helper                                  1   0%
             - js2-parse-name-or-label                              1   0%
              - js2-parse-expr                                      1   0%
               - js2-parse-assign-expr                              1   0%
                - js2-parse-assign-expr                             1   0%
                 - js2-get-token                                    1   0%
                  - js2-get-token-internal                          1   0%
                   - js2-get-token-internal-1                       1   0%
                    - js2-identifier-start-p                        1   0%
                       get-char-code-property                       1   0%
       - eval                                                       1   0%
        - lsp-mode-line                                             1   0%
         - mapcar                                                   1   0%
          - #<compiled 0xada8ed5251bda93>                           1   0%
             lsp--workspace-print                                   1   0%
       read-event                                                   1   0%
     mwheel-scroll                                                 20   5%
   - save-buffer                                                    2   0%
    - basic-save-buffer                                             2   0%
     - run-hooks                                                    2   0%
      - py-autopep8-buffer                                          1   0%
         py-autopep8-bf--apply-executable-to-buffer                  1   0%
      - flycheck-handle-save                                        1   0%
       - flycheck-buffer-automatically                              1   0%
        - flycheck-buffer                                           1   0%
         - flycheck-start-current-syntax-check                      1   0%
            flycheck-compute-working-directory                      1   0%
  - byte-code                                                      69  17%
   - read-extended-command                                         69  17%
    - completing-read                                              69  17%
     - completing-read-default                                     69  17%
      - read-from-minibuffer                                       63  16%
       - command-execute                                           17   4%
        - call-interactively                                       17   4%
         - funcall-interactively                                   17   4%
          - minibuffer-complete                                    17   4%
           - completion-in-region                                  17   4%
            - completion--in-region                                17   4%
             - #<compiled -0x1c4b2cd50cff4fab>                     17   4%
              - apply                                              17   4%
               - #<compiled 0x1d495fcb212b6988>                    17   4%
                - completion--in-region-1                          17   4%
                 - completion--do-completion                       17   4%
                  - minibuffer-completion-help                     14   3%
                   - completion-all-completions                     7   1%
                    - completion--nth-completion                    7   1%
                     - completion--some                             7   1%
                      - #<compiled -0x72f8df7eacf51ff>                  7   1%
                       + completion-basic-all-completions                  7   1%
                   - temp-buffer-window-show                        5   1%
                    - display-buffer                                5   1%
                     + display-buffer-at-bottom                     5   1%
                     fit-window-to-buffer                           1   0%
                   + mapcar                                         1   0%
                  + completion-try-completion                       3   0%
         minibuffer-inactive-mode                                   1   0%
       - timer-event-handler                                        1   0%
        + apply                                                     1   0%
- ...                                                              48  12%
   Automatic GC                                                    42  10%
 - js2-parse-and-expr                                               6   1%
  - js2-parse-bit-or-expr                                           6   1%
   - js2-parse-bit-xor-expr                                         6   1%
    - js2-parse-bit-and-expr                                        6   1%
     - js2-parse-eq-expr                                            6   1%
      - js2-parse-rel-expr                                          6   1%
       - js2-parse-shift-expr                                       6   1%
        - js2-parse-add-expr                                        6   1%
         - js2-parse-mul-expr                                       6   1%
          - js2-parse-expon-expr                                    6   1%
           - js2-parse-unary-expr                                   6   1%
            - js2-parse-member-expr                                 6   1%
             + js2-parse-primary-expr                               6   1%
- timer-event-handler                                              45  11%
 - apply                                                           44  11%
  - ac-update-greedy                                               39  10%
   - ac-update                                                     39  10%
    - ac-menu-create                                               39  10%
     - popup-create                                                39  10%
        popup-vertical-motion                                       1   0%
        beginning-of-visual-line                                    1   0%
    #<compiled 0x886bf5cf614a9>                                     2   0%
  + deferred:worker                                                 2   0%
  + flycheck--handle-idle-trigger                                   1   0%
 + timer-inc-time                                                   1   0%
- redisplay_internal (C function)                                  21   5%
 - #<lambda 0xb35c3b8d32610e>                                      16   4%
  - highlight-indentation-redraw-window                            16   4%
     highlight-indentation-redraw-region                            6   1%
 + eval                                                             4   1%
 + jit-lock-function                                                1   0%

Slow downs are generally due to jedi analysing the code and the imported packages.

Do you have a minimal example for which it happens ?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kapilsh picture kapilsh  Â·  3Comments

SergiyKolesnikov picture SergiyKolesnikov  Â·  3Comments

kirk86 picture kirk86  Â·  5Comments

Arseny-N picture Arseny-N  Â·  4Comments

CDitzel picture CDitzel  Â·  5Comments