-
-
Notifications
You must be signed in to change notification settings - Fork 295
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
Call Django Class based view function with Django-Q's async_task #463
Comments
As a quick fix you could try to async the import path i.e. "Greeter.greet"
…On Mon, 3 Aug 2020, 09:54 Alexis Richter, ***@***.***> wrote:
I can't call async_task on a Django class based view function. Considering
following case, where I have a DRF view:
class Greeting(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.greeting) 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.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#463>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AA6AQNOKJGHJJ5SODZTIITTR6ZUMJANCNFSM4PTCGLVA>
.
|
I tried that as well, but then I have a |
That probably means you are not giving it the full import parth ie: "myapp.Greeter.greet" |
hi |
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 ***@***.***> 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
<#463 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AA6AQNPHESQ2AS5UZ74X7OTSAJGWHANCNFSM4PTCGLVA>
.
|
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:
Of course, if you need a class instance you need to instantiate it first in the |
I can't call async_task on a Django class based view function. Considering following case, where I have a DRF view:
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'
errorI have no issues when putting the
greet
function outside de class.edit: correcting function names
The text was updated successfully, but these errors were encountered: