Skip to content

[Docs] Add usage instructions for Django (including 3.x) #224

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

Closed
pinguin999 opened this issue Oct 7, 2020 · 5 comments
Closed

[Docs] Add usage instructions for Django (including 3.x) #224

pinguin999 opened this issue Oct 7, 2020 · 5 comments

Comments

@pinguin999
Copy link

pinguin999 commented Oct 7, 2020

Hi I'm trying the django example from: https://devblogs.microsoft.com/python/announcing-playwright-for-python-reliable-end-to-end-testing-for-the-web/

But I'm getting this error:

Creating test database for alias 'default'...
System check identified no issues (1 silenced).
.E
======================================================================
ERROR: test_login (app.tests_playwright.MyViewTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\Lubitz\.virtualenvs\DjangoBasisLager-JHlOkGlw\lib\site-packages\django\test\testcases.py", line 274, in __call__
    self._post_teardown()
  File "C:\Users\Lubitz\.virtualenvs\DjangoBasisLager-JHlOkGlw\lib\site-packages\django\test\testcases.py", line 1009, in _post_teardown
    self._fixture_teardown()
  File "C:\Users\Lubitz\.virtualenvs\DjangoBasisLager-JHlOkGlw\lib\site-packages\django\test\testcases.py", line 1044, in _fixture_teardown
    inhibit_post_migrate=inhibit_post_migrate)
  File "C:\Users\Lubitz\.virtualenvs\DjangoBasisLager-JHlOkGlw\lib\site-packages\django\core\management\__init__.py", line 168, in call_command
    return command.execute(*args, **defaults)
  File "C:\Users\Lubitz\.virtualenvs\DjangoBasisLager-JHlOkGlw\lib\site-packages\django\core\management\base.py", line 369, in execute
    output = self.handle(*args, **options)
  File "C:\Users\Lubitz\.virtualenvs\DjangoBasisLager-JHlOkGlw\lib\site-packages\django\core\management\commands\flush.py", line 49, in handle
    allow_cascade=allow_cascade)
  File "C:\Users\Lubitz\.virtualenvs\DjangoBasisLager-JHlOkGlw\lib\site-packages\django\core\management\sql.py", line 13, in sql_flush
    tables = connection.introspection.django_table_names(only_existing=True, include_views=False)
  File "C:\Users\Lubitz\.virtualenvs\DjangoBasisLager-JHlOkGlw\lib\site-packages\django\db\backends\base\introspection.py", line 85, in django_table_names
    existing_tables = set(self.table_names(include_views=include_views))
  File "C:\Users\Lubitz\.virtualenvs\DjangoBasisLager-JHlOkGlw\lib\site-packages\django\db\backends\base\introspection.py", line 46, in table_names
    with self.connection.cursor() as cursor:
  File "C:\Users\Lubitz\.virtualenvs\DjangoBasisLager-JHlOkGlw\lib\site-packages\django\utils\asyncio.py", line 24, in inner
    raise SynchronousOnlyOperation(message)
django.core.exceptions.SynchronousOnlyOperation: You cannot call this from an async context - use a thread or sync_to_async.

----------------------------------------------------------------------
Ran 1 test in 2.323s

FAILED (errors=1)
Destroying test database for alias 'default'...
```
@arjunattam
Copy link
Contributor

arjunattam commented Oct 7, 2020

Thanks for reporting this @pinguin999. I was testing on Django 2.x while writing the blog post, and I suspect that you're using Django 3. I'll fix the post for 3.x.

@mxschmitt and I were trying this on Django 3, and he put together this sample project that requires the DJANGO_ALLOW_ASYNC_UNSAFE=1 environment variable to run the test without throwing this error.

Would it be possible for you to try with this env var? We would need to understand whether there are any side-effects.

@pinguin999
Copy link
Author

Hi looks good.

I added os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true" in def setUpClass(cls): and now it works

Thanks to @arjun27 and @mxschmitt

@mxschmitt
Copy link
Member

mxschmitt commented Oct 22, 2020

Closed since the workaround seems to be working for now. Thanks for reporting and confirming that its working.

@tyomo4ka
Copy link

@mxschmitt

I might be wrong, but as far as I understand this is what happens:

  • playwright creates an event loop
  • Django 3.* has a validation that prevents running sync operations (e.g. model queries) inside an event loop
  • DJANGO_ALLOW_ASYNC_UNSAFE=true disables this validation
  • running long sync operation inside an event loop blocks the entire loop so that's why this validation exists in Django
  • in test environment blocking the event loop is ok

Maybe it's a good idea to document this workaround?

@arjunattam arjunattam changed the title Django SynchronousOnlyOperation(message) [Docs] Add usage instructions for Django (including 3.x) Oct 28, 2020
@arjunattam arjunattam reopened this Oct 28, 2020
@arjunattam arjunattam self-assigned this Oct 28, 2020
@pavelfeldman
Copy link
Member

Joining with #439

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

No branches or pull requests

5 participants