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

Fix docs example to prevent SynchronousOnlyOperation in async consumers #204

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

virgil724
Copy link

Description

This PR updates the documentation example for GenericAsyncAPIConsumer to prevent potential SynchronousOnlyOperation exceptions that can occur when accessing serializers.data in an asynchronous context.

Changes

  • Updated comment_activity method to use dict(message) instead of dict(message.data)
  • Modified comment_activity serializer method to return ReturnDict instead of CommentSerializer
  • Changed the serializer method to return serializer.data directly, avoiding database queries in the async context

Motivation

The original example could lead to database queries being executed in an asynchronous context when accessing serializers.data, potentially causing django.core.exceptions.SynchronousOnlyOperation exceptions. This fix ensures that the serialization happens in a synchronous context, making the example safer for use in asynchronous consumers.

Fix docs example to prevent SynchronousOnlyOperation exception

- Update comment_activity method to use dict(message) instead of dict(message.data)
- Modify comment_activity serializer to return ReturnDict instead of CommentSerializer
- Return serializer.data in the serializer method to avoid database queries in async context

This change addresses the issue where serializers.data can trigger database queries,
potentially causing django.core.exceptions.SynchronousOnlyOperation exceptions in
asynchronous consumers.
@virgil724 virgil724 changed the title Fix docs example to prevent SynchronousOnlyOperation exception - Update comment_activity method to use dict(message) instead of dict(message.data) - Modify comment_activity serializer to return ReturnDict instead of CommentSerializer - Return serializer.data in the serializer method to avoid database queries in async context This change addresses the issue where serializers.data can trigger database queries, potentially causing django.core.exceptions.SynchronousOnlyOperation exceptions in asynchronous consumers. Fix docs example to prevent SynchronousOnlyOperation in async consumers Jul 8, 2024
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

Successfully merging this pull request may close these issues.

1 participant