Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Scalene CLI does not work with Django application development server #178

Closed
pjbull opened this issue May 3, 2021 · 1 comment
Closed

Comments

@pjbull
Copy link

pjbull commented May 3, 2021

Describe the bug
The normal process for running a Django application while developing it is to run python manage.py runserver, which runs a development server until the user hits Ctrl+C to end that server.

Ideally, I would be able to run scalene manage.py runserver, run some tests, hit Ctrl+C and have scalene report on everything that happened during that session.

Currently, running scalene manage.py runserver does not keep the server running and just stops the process.

There is a workaround to this issue (see below), so documenting rather than fixing may be sufficient.

To Reproduce

Here are the steps to reproduce using an implementation of a Django app from their official tutorial:

git clone https://github.com/devmahmud/Django-Poll-App
cd Django-Poll-App

# activate a virtualenv, if you want
pip install django scalene  
python manage.py makemigrations && python manage.py migrate   # setup sqlite database

# runs server successfully (see at http://127.0.0.1:8000/)
python manage.py runserver

# process doesn't keep running
scalene manage.py runserver

Expected behavior
Ideally, I would be able to run scalene manage.py runserver, run some tests, hit Ctrl+C and have scalene report on everything that happened during that session.

Screenshots

Here are the logs from both calls

Django-Poll-App on  master 🐍 v3.9.4 via 🅒 Django-Poll-App
❯ python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified some issues:

WARNINGS:
polls.Choice: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'.
	HINT: Configure the DEFAULT_AUTO_FIELD setting or the PollsConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'.
polls.Poll: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'.
	HINT: Configure the DEFAULT_AUTO_FIELD setting or the PollsConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'.
polls.Vote: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'.
	HINT: Configure the DEFAULT_AUTO_FIELD setting or the PollsConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'.

System check identified 3 issues (0 silenced).
May 04, 2021 - 01:21:49
Django version 3.2, using settings 'pollme.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[04/May/2021 01:21:56] "GET / HTTP/1.1" 200 2880
[04/May/2021 01:21:57] "GET /static/css/home_style.css HTTP/1.1" 200 280
[04/May/2021 01:21:57] "GET /static/img/background.jpg HTTP/1.1" 200 2910796
Not Found: /favicon.ico
[04/May/2021 01:21:57] "GET /favicon.ico HTTP/1.1" 404 2323
^C%
Django-Poll-App on  master 🐍 v3.9.4 via 🅒 Django-Poll-App took 26s
❯ scalene manage.py runserver
30413
/var/folders/8g/v8lwvfhj6_l6ct_zd_rs84mw0000gn/T/scalene_ocdo9g5/python: line 3: syntax error near unexpected token `('
/var/folders/8g/v8lwvfhj6_l6ct_zd_rs84mw0000gn/T/scalene_ocdo9g5/python: line 3: `    PYTHONMALLOC=malloc DYLD_INSERT_LIBRARIES=<REDACTED>/lib/python3.9/site-packages/scalene/libscalene.dylib <REDACTED>/bin/python -m scalene  --cpu-sampling-rate=0.01 --pid={os.getpid()} $@'
                                               Memory usage: ▆▇▇██████████████████ (max:  30.49MB, growth rate: 100%)
                                                           manage.py: % of time = 100.00% out of   0.45s.
       ╷       ╷       ╷       ╷        ╷       ╷               ╷       ╷
  Line │Time   │–––––– │–––––– │Memory  │–––––– │–––––––––––    │Copy   │
       │Python │native │system │Python  │net    │timeline/%     │(MB/s) │manage.py
╺━━━━━━┿━━━━━━━┿━━━━━━━┿━━━━━━━┿━━━━━━━━┿━━━━━━━┿━━━━━━━━━━━━━━━┿━━━━━━━┿━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╸
     1 │       │       │       │        │       │               │       │#!/usr/bin/env python
     2 │       │       │       │        │       │               │       │import os
     3 │       │       │       │        │       │               │       │import sys
     4 │       │       │       │        │       │               │       │
     5 │       │       │       │        │       │               │       │if __name__ == '__main__':
     6 │       │       │       │        │       │               │       │    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'pollme.settings')
     7 │       │       │       │        │       │               │       │    try:
     8 │   10% │    3% │   2%  │  20%   │    3M │▁▁▁  20%       │    22 │        from django.core.management import execute_from_command_line
     9 │       │       │       │        │       │               │       │    except ImportError as exc:
    10 │       │       │       │        │       │               │       │        raise ImportError(
    11 │       │       │       │        │       │               │       │            "Couldn't import Django. Are you sure it's installed and "
    12 │       │       │       │        │       │               │       │            "available on your PYTHONPATH environment variable? Did you "
    13 │       │       │       │        │       │               │       │            "forget to activate a virtual environment?"
    14 │       │       │       │        │       │               │       │        ) from exc
    15 │   53% │    6% │  26%  │  73%   │   11M │▄▅▆▆▆  73%     │    18 │    execute_from_command_line(sys.argv)
       ╵       ╵       ╵       ╵        ╵       ╵               ╵       ╵
Top net memory consumption, by line:
(1)    15:    11 MB
(2)     8:     3 MB
                                        <REDACTED>/Django-Poll-App/polls/admin.py: % of time =   0.00% out of   0.45s.
       ╷       ╷       ╷       ╷        ╷       ╷               ╷       ╷
  Line │Time   │–––––– │–––––– │Memory  │–––––– │–––––––––––    │Copy   │
       │Python │native │system │Python  │net    │timeline/%     │(MB/s) │<REDACTED>/Django-Poll-App/polls/admin.py
╺━━━━━━┿━━━━━━━┿━━━━━━━┿━━━━━━━┿━━━━━━━━┿━━━━━━━┿━━━━━━━━━━━━━━━┿━━━━━━━┿━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╸
     1 │       │       │       │        │       │               │       │from django.contrib import admin
     2 │       │       │       │        │       │               │       │from .models import Poll, Choice, Vote
     3 │       │       │       │        │       │               │       │
     4 │       │       │       │        │       │               │       │admin.site.register(Poll)
     5 │       │       │       │        │       │               │       │admin.site.register(Choice)
     6 │       │       │       │   7%   │    1M │▁   7%         │       │admin.site.register(Vote)
       ╵       ╵       ╵       ╵        ╵       ╵               ╵       ╵
Top net memory consumption, by line:

Desktop (please complete the following information):

  • OS: MacOS
  • Version 10.15.7

WORKAROUND

There is a workaround, and maybe it is just worth documenting it. If you pass the --noreload option, things work as expected.

It may be the case that the Django autoreload functionality causes this problem because the main django work happens on a Daemon thread if --noreload is not passed (not clear if there are alsoe additional subprocesses, but this seemed to be the case in my testing):

https://github.com/django/django/blob/ca9872905559026af82000e46cde6f7dedc897b6/django/utils/autoreload.py#L611-L614

@emeryberger
Copy link
Member

Adding to the docs, thanks!

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants