Call Django Class based view function with Django-Q's async_task
See original GitHub issueI can’t call async_task on a Django class based view function. Considering following case, where I have a DRF view:
class Greeter(generics.GenericAPIView)
def greet(self, name):
print("Hello: ")
sleep(5)
print(name)
def post(self, request, *args, **kwargs):
# enqueue the task
async_task(self.greet, "world")
return Response(
{"message": "you should see this right away"}, status=status.HTTP_200_OK
)
When trying to call async_task on a Django class based view function, following errors occur:
async_task(self.greet)
returns ancannot pickle '_io.BufferedReader' object
errorasync_task("self.greet")
returns anNo module named 'self'
error
I have no issues when putting the greet
function outside de class.
edit: correcting function names
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Asynchronous support - Django documentation
Any view can be declared async by making the callable part of it return a coroutine - commonly, this is done using async...
Read more >Async Views in Django - TestDriven.io
This tutorial looks at how to get started with Django's asynchronous views.
Read more >How Django Currently Handles Asynchronous Views
With the Django 3.1 release, Django now supports async views, so if you are running ASGI, writing async specific tasks is now possible!...
Read more >Django Async Class Based Views (ACBV) | by Bruno Fosados
Django Async Class Based Views (ACBV). Hello there! Django 3.1 finally was released with support for Class Based Views and Function Based Views, ......
Read more >Asynchronous Tasks With Django and Celery - Real Python
What Celery beat adds to the mix is a time-based scheduler for Celery workers. In this tutorial, you'll learn how to integrate Celery...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Just create a separate task for this:
ie
def print_test(test: Test): return test.print_test()
and then async this function with the Test() instance
async_task(my_module.tasks.print_test, Test())
Only the arguments can be instances and will be serialized.
On Wed, Aug 12, 2020 at 10:25 AM mostafa khaki notifications@github.com wrote:
I too have a class with many static methods and I wish to call the class functions. Instead of creating one separate task for each class function, I created one task that will run the class functions:
Then you may call it like:
async_task("my_proj.run_task", "class_static_function", arg1)
Of course, if you need a class instance you need to instantiate it first in the
run_task
. If you also need to pass class args, you probably need a dict arg for the class args and other for the function args.