Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

3.4.1 RuntimeError #292

Closed
belongwqz opened this issue Aug 18, 2021 · 11 comments
Closed

3.4.1 RuntimeError #292

belongwqz opened this issue Aug 18, 2021 · 11 comments

Comments

@belongwqz
Copy link

belongwqz commented Aug 18, 2021

1.pip install django
Successfully installed asgiref-3.4.1 django-3.2.6 pytz-2021.1 sqlparse-0.4.1
2.django-admin startproject Sample

Traceback (most recent call last):
  File "/django_venv/bin/django-admin", line 6, in <module>
    from django.core.management import execute_from_command_line
  File "/django_venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 13, in <module>
    from django.apps import apps
  File "/django_venv/lib/python3.8/site-packages/django/apps/__init__.py", line 1, in <module>
    from .config import AppConfig
  File "/django_venv/lib/python3.8/site-packages/django/apps/config.py", line 7, in <module>
    from django.utils.deprecation import RemovedInDjango41Warning
 File "/django_venv/lib/python3.8/site-packages/django/utils/deprecation.py", line 5, in <module>
    from asgiref.sync import sync_to_async
  File "/django_venv/lib/python3.8/site-packages/asgiref/sync.py", line 304, in <module>
    class SyncToAsync:
  File "/django_venv/lib/python3.8/site-packages/asgiref/sync.py", line 328, in SyncToAsync
    loop = get_running_loop()
RuntimeError: no running event loop

But use version 3.3.4 will be OK.

@andrewgodwin
Copy link
Member

I cannot replicate this on my side - it works exactly as expected. Can you provide a full pip freeze of your package list, tell us your OS and architecture, and show us what command you are running?

@belongwqz
Copy link
Author

belongwqz commented Aug 18, 2021

I cannot replicate this on my side - it works exactly as expected. Can you provide a full pip freeze of your package list, tell us your OS and architecture, and show us what command you are running?

env info as follow:

python version:3.8.2

OS:Red Hat Enterprise Linux Server 2.6.32-431.el6.x86_64

pip freeze:
asgiref==3.4.1
Django==3.2.6
pytz==2021.1
sqlparse==0.4.1

I just create a venv use command python -m venv django_venv, then activate itcd django_venv;. bin/activate.
After that, I install django use command pip install django, at last execute django-admin startproject Sample.
If I downgrade to version 3.3.4pip install asgiref==3.3.4, then the startproject commond success.

What's more, I try version 3.4.1 on windows and it works. Looks only linux has this problem.

@carltongibson
Copy link
Member

I can reproduce this. It's because you have the ASGI_THREADS environment variable set, and the offending get_running_loop() is called at class definition in that case:

~ $ python --version
Python 3.9.6
~ $ mktmpenv
...
This is a temporary environment. It will be deleted when you run 'deactivate'.
(tmp-98dde6625b183cc) ~/ve/tmp-98dde6625b183cc $ pip install Django
Collecting Django
  Using cached Django-3.2.6-py3-none-any.whl (7.9 MB)
Collecting sqlparse>=0.2.2
  Using cached sqlparse-0.4.1-py3-none-any.whl (42 kB)
Collecting asgiref<4,>=3.3.2
  Using cached asgiref-3.4.1-py3-none-any.whl (25 kB)
Collecting pytz
  Using cached pytz-2021.1-py2.py3-none-any.whl (510 kB)
Installing collected packages: sqlparse, pytz, asgiref, Django
Successfully installed Django-3.2.6 asgiref-3.4.1 pytz-2021.1 sqlparse-0.4.1
WARNING: You are using pip version 21.1.3; however, version 21.2.4 is available.
You should consider upgrading via the '/Users/carlton/ve/tmp-98dde6625b183cc/bin/python -m pip install --upgrade pip' command.
(tmp-98dde6625b183cc) ~/ve/tmp-98dde6625b183cc $ django-admin startproject Sample
(tmp-98dde6625b183cc) ~/ve/tmp-98dde6625b183cc $ ls
README.tmpenv Sample        bin           lib           pyvenv.cfg
(tmp-98dde6625b183cc) ~/ve/tmp-98dde6625b183cc $ rm -rf Sample
(tmp-98dde6625b183cc) ~/ve/tmp-98dde6625b183cc $ tail -n +326 ~/Documents/Django-Stack/asgiref/asgiref/sync.py | head -n 7
    # If they've set ASGI_THREADS, update the default asyncio executor for now
    if "ASGI_THREADS" in os.environ:
        loop = get_running_loop()
        loop.set_default_executor(
            ThreadPoolExecutor(max_workers=int(os.environ["ASGI_THREADS"]))
        )

(tmp-98dde6625b183cc) ~/ve/tmp-98dde6625b183cc $ ASGI_THREADS=4 django-admin startproject Sample
Traceback (most recent call last):
    ...FRAMES OMITTED... 
  File "/Users/carlton/ve/tmp-98dde6625b183cc/lib/python3.9/site-packages/django/utils/deprecation.py", line 5, in <module>
    from asgiref.sync import sync_to_async
  File "/Users/carlton/ve/tmp-98dde6625b183cc/lib/python3.9/site-packages/asgiref/sync.py", line 304, in <module>
    class SyncToAsync:
  File "/Users/carlton/ve/tmp-98dde6625b183cc/lib/python3.9/site-packages/asgiref/sync.py", line 328, in SyncToAsync
    loop = get_running_loop()
RuntimeError: no running event loop

asgiref/asgiref/sync.py

Lines 329 to 334 in f53fbef

# If they've set ASGI_THREADS, update the default asyncio executor for now
if "ASGI_THREADS" in os.environ:
loop = get_running_loop()
loop.set_default_executor(
ThreadPoolExecutor(max_workers=int(os.environ["ASGI_THREADS"]))
)

@belongwqz
Copy link
Author

I can reproduce this. It's because you have the ASGI_THREADS environment variable set, and the offending get_running_loop() is called at class definition in that case:

~ $ python --version
Python 3.9.6
~ $ mktmpenv
...
This is a temporary environment. It will be deleted when you run 'deactivate'.
(tmp-98dde6625b183cc) ~/ve/tmp-98dde6625b183cc $ pip install Django
Collecting Django
  Using cached Django-3.2.6-py3-none-any.whl (7.9 MB)
Collecting sqlparse>=0.2.2
  Using cached sqlparse-0.4.1-py3-none-any.whl (42 kB)
Collecting asgiref<4,>=3.3.2
  Using cached asgiref-3.4.1-py3-none-any.whl (25 kB)
Collecting pytz
  Using cached pytz-2021.1-py2.py3-none-any.whl (510 kB)
Installing collected packages: sqlparse, pytz, asgiref, Django
Successfully installed Django-3.2.6 asgiref-3.4.1 pytz-2021.1 sqlparse-0.4.1
WARNING: You are using pip version 21.1.3; however, version 21.2.4 is available.
You should consider upgrading via the '/Users/carlton/ve/tmp-98dde6625b183cc/bin/python -m pip install --upgrade pip' command.
(tmp-98dde6625b183cc) ~/ve/tmp-98dde6625b183cc $ django-admin startproject Sample
(tmp-98dde6625b183cc) ~/ve/tmp-98dde6625b183cc $ ls
README.tmpenv Sample        bin           lib           pyvenv.cfg
(tmp-98dde6625b183cc) ~/ve/tmp-98dde6625b183cc $ rm -rf Sample
(tmp-98dde6625b183cc) ~/ve/tmp-98dde6625b183cc $ tail -n +326 ~/Documents/Django-Stack/asgiref/asgiref/sync.py | head -n 7
    # If they've set ASGI_THREADS, update the default asyncio executor for now
    if "ASGI_THREADS" in os.environ:
        loop = get_running_loop()
        loop.set_default_executor(
            ThreadPoolExecutor(max_workers=int(os.environ["ASGI_THREADS"]))
        )

(tmp-98dde6625b183cc) ~/ve/tmp-98dde6625b183cc $ ASGI_THREADS=4 django-admin startproject Sample
Traceback (most recent call last):
    ...FRAMES OMITTED... 
  File "/Users/carlton/ve/tmp-98dde6625b183cc/lib/python3.9/site-packages/django/utils/deprecation.py", line 5, in <module>
    from asgiref.sync import sync_to_async
  File "/Users/carlton/ve/tmp-98dde6625b183cc/lib/python3.9/site-packages/asgiref/sync.py", line 304, in <module>
    class SyncToAsync:
  File "/Users/carlton/ve/tmp-98dde6625b183cc/lib/python3.9/site-packages/asgiref/sync.py", line 328, in SyncToAsync
    loop = get_running_loop()
RuntimeError: no running event loop

asgiref/asgiref/sync.py

Lines 329 to 334 in f53fbef

# If they've set ASGI_THREADS, update the default asyncio executor for now
if "ASGI_THREADS" in os.environ:
loop = get_running_loop()
loop.set_default_executor(
ThreadPoolExecutor(max_workers=int(os.environ["ASGI_THREADS"]))
)

Yes, it is, thanks!

@carltongibson
Copy link
Member

Hey @belongwqz — Glad that's got you moving!

I'm going to reopen to let @andrewgodwin decide whether an adjustment is needed here. Presumably setting ASGI_THREADS is valid, and this class module can get imported before the event loop starts.

@andrewgodwin
Copy link
Member

Thanks Carlton - that was indeed the problem. I've just pushed up a fix, will try and get it into a release soon.

@carltongibson
Copy link
Member

Thanks @andrewgodwin 🥇

@variable
Copy link

variable commented Sep 1, 2021

@carltongibson

I am not sure if this is solely because of the ASGI_THREADS environment settings, cos it also gives the same error on when running manage.py runworker <channel> and there is no ASGI_THREADS defined.

Running worker for channels ['multiplex']
INFO  2021-09-01 12:08:59,526  runworker 17875  140146458650432  Running worker for channels ['multiplex']
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/srv/www/portal/lib/python3.8/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/srv/www/portal/lib/python3.8/site-packages/django/core/management/__init__.py", line 375, in execute 
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/srv/www/portal/lib/python3.8/site-packages/django/core/management/base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/srv/www/portal/lib/python3.8/site-packages/django/core/management/base.py", line 364, in execute 
    output = self.handle(*args, **options)
  File "/srv/www/portal/lib/python3.8/site-packages/channels/management/commands/runworker.py", line 46, in handle
    worker.run()
  File "/srv/www/portal/lib/python3.8/site-packages/asgiref/server.py", line 61, in run
    event_loop = get_running_loop()
RuntimeError: no running event loop

@variable
Copy link

variable commented Sep 1, 2021

ahh nevermind, seems this is duplicate of #278

@aaronmader
Copy link

Any idea when this fix will be released?

@andrewgodwin
Copy link
Member

I just have to get around to it - not sure when. Pandemic has put everything out of kilter.

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

No branches or pull requests

5 participants