Open seanpar203 opened 4 years ago
I guess a solution is to create a function that does the work and have the task call that function, that way the function can be tested outside the scope of a task but still have the proper close_connection
functionality when in production?
def calculate_money():
# Do something
return None
@app.task
@close_connection
def task_calculate_money():
calculate_money()
Although this isn't ideal obviously.
Hi!, Have you tried with any of the others methods in the readme instead of using the decorator?
I had a similar issue when using pytest.mark.django_db
. Ultimately, I didn't use close_connection
that comes with django-db-geventpool
, but rolled my own that only closed the connection if not in a transaction (which pytest.mark.django_db
performs tests in). Something that did:
from django.db import connection
if not connection.in_atomic_block:
connection.close_if_unusable_or_obsolete()
Not great, since I'm not such a fan having code specifically for the reason that it behaves differently in the tests than in production, but it may be the least-bad option.
Following the documentation of decorating celery tasks with
@close_connection
, I've had some difficult with running django tests usingpytest
as the runner.Here's the code(it's pretty simple)
test.py
tasks.py
When running pytest I get these errors:
The test also fails but when I paste the task functionality inside the test and don't actually call the task function, the test passes.
Is this expected or am I doing something wrong?