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

[Django] You cannot call this from an async context - use a thread or sync_to_async. #36

Closed
valeedmalik opened this issue Dec 28, 2020 · 7 comments

Comments

@valeedmalik
Copy link

If I run manage.py test, that will work as expected however when running via pycrunch IJ plugin I see this:
django.core.exceptions.SynchronousOnlyOperation: You cannot call this from an async context - use a thread or sync_to_async.

django==3.1.4

actual test:

from django.test import TestCase

class Entire_Upload(TestCase):
  def setUp(self):
    pass

  def test_mov(self):
    pass

full error output:

E
==================================== ERRORS ====================================
___________________ ERROR at setup of Entire_Upload.test_mov ___________________

cls = <class 'tests.test_video_upload.Entire_Upload'>

    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        if not cls._databases_support_transactions():
            return
>       cls.cls_atomics = cls._enter_atomics()

/usr/local/lib/python3.7/site-packages/django/test/testcases.py:1112: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
/usr/local/lib/python3.7/site-packages/django/test/testcases.py:1093: in _enter_atomics
    atomics[db_name].__enter__()
/usr/local/lib/python3.7/site-packages/django/db/transaction.py:175: in __enter__
    if not connection.get_autocommit():
/usr/local/lib/python3.7/site-packages/django/db/backends/base/base.py:389: in get_autocommit
    self.ensure_connection()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

args = (<django.db.backends.postgresql.base.DatabaseWrapper object at 0x7f0c4139fdd0>,)
kwargs = {}
event_loop = <_UnixSelectorEventLoop running=True closed=False debug=False>

    @functools.wraps(func)
    def inner(*args, **kwargs):
        if not os.environ.get('DJANGO_ALLOW_ASYNC_UNSAFE'):
            # Detect a running event loop in this thread.
            try:
                event_loop = asyncio.get_event_loop()
            except RuntimeError:
                pass
            else:
                if event_loop.is_running():
>                   raise SynchronousOnlyOperation(message)
E                   django.core.exceptions.SynchronousOnlyOperation: You cannot call this from an async context - use a thread or sync_to_async.

/usr/local/lib/python3.7/site-packages/django/utils/asyncio.py:24: SynchronousOnlyOperation
=========================== short test summary info ============================
ERROR apps/src/tests/test_video_upload.py::Entire_Upload::test_mov - django.c...
1 error in 0.19s

@valeedmalik
Copy link
Author

valeedmalik commented Dec 29, 2020

python-engineio==3.12.1
python-socketio==4.5.1

as per https://plugins.jetbrains.com/plugin/13264-pycrunch--live-testing/reviews

@valeedmalik
Copy link
Author

upon triggering test

INFO Running tests...
INFO collecting 1 tests for run
DEBUG Received task in queue
INFO tests_will_run
INFO notify_clients_about_tests_change
INFO total_tests_to_run 1
INFO process_single_message - handshake
[25] [task_id: afed567f-0fcd-427a-98fa-2ac767d96218] Data received: test-run-task;
INFO process_single_message - test_run_results
INFO process_single_message - timings
INFO process_single_message - close
[25] [task_id: afed567f-0fcd-427a-98fa-2ac767d96218] - The connection to parent pycrunch-engine process lost
WARNING Executing <Handle BaseSelectorEventLoop._read_from_self()> took 0.188 seconds
INFO notify_clients_about_tests_change

@gleb-sevruk
Copy link
Owner

Hello, thanks for detailed report.

I was able to reproduce the issue using the latest Django (3.1.4)

I will provide more details after investigation.

@gleb-sevruk
Copy link
Owner

Quick workaround would be setting DJANGO_ALLOW_ASYNC_UNSAFE env variable in .pycrunch-config.yaml, such as:

engine:
    runtime: django
env:
  DJANGO_SETTINGS_MODULE: django31.settings
  DJANGO_ALLOW_ASYNC_UNSAFE: "true"

@valeedmalik
Copy link
Author

Thanks that will work my purposes. I appreciate the quick response.

@gleb-britecore gleb-britecore pinned this issue Jul 21, 2021
@shakori999
Copy link

shakori999 commented Dec 30, 2021

I have the same error , but I don't have the file! how can I solve it?

@gleb-sevruk
Copy link
Owner

@shakori999 this file will be created at the first run of the engine in the root directory of the project.

You might have an old version of package (when configuration file is not created automatically), make sure to update via
pip install --upgrade pycrunch-engine

more at:
https://pycrunch.com/docs/configuration-file

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

3 participants