question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Call Django Class based view function with Django-Q's async_task

See original GitHub issue

I 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 an cannot pickle '_io.BufferedReader' object error
  • async_task("self.greet") returns an No module named 'self' error

I have no issues when putting the greet function outside de class.

edit: correcting function names

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
Koed00commented, Aug 12, 2020

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:

hi how in case that we need to create an instance of an object then pass that function from the instance, set that function to async_task? ex: async_task(Test().print_test) but error some pickle error

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/Koed00/django-q/issues/463#issuecomment-672728311, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA6AQNPHESQ2AS5UZ74X7OTSAJGWHANCNFSM4PTCGLVA .

0reactions
rafael-h-ferreiracommented, Oct 6, 2021

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:

from my_proj import ProjClass
def run_task(class_static_method: str, *args, **kwargs):
    func = getattr(ProjClass, class_static_method)
    return func(*args, **kwargs)

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found