Code Monkey home page Code Monkey logo

Comments (6)

carltongibson avatar carltongibson commented on August 24, 2024 1

Compare your class:

https://github.com/DevGauravJoshi/python_async/blob/eb0cfb1894fcaaa1aaeed245a032ff1c6ecd40c3/backend/chats/consumers.py#L180-L182

to the base class:

@classmethod
async def encode_json(cls, content):
return json.dumps(content)

By overriding this method without using the async keyword, the await call at the error point is receiving a string, rather than a coroutine object. You want to pass UUIDEncoder, but you still need to declare the method async def.

from channels.

gaurav-jo1 avatar gaurav-jo1 commented on August 24, 2024

After examining the code within the parent class (AsyncJsonWebsocketConsumer), I noticed that the encoding process for the content using json.dumps(content) was utilizing the await keyword. However, I found that removing the await and simply using json.dumps resolved the error. This adjustment allowed the "welcome message" to be sent successfully to the client side without encountering any error.

from channels.

carltongibson avatar carltongibson commented on August 24, 2024

The encode_json method is unchanged since it was introduced:

de82aaa#diff-8adaf2e6e01ec2cc05dd10bfd212d6dddfadabe0ddc679b821e03024806647b7R254-R256

    @classmethod
     async def encode_json(cls, content):
         return json.dumps(content)

from channels.

gaurav-jo1 avatar gaurav-jo1 commented on August 24, 2024

encode_json is defined as an async function, but it uses json.dumps which is a synchronous function.

Error explanation:

When we call await self.encode_json(content), it treats the return value of json.dumps(content) as a coroutine, but since json.dumps returns a string synchronously, it raises the TypeError.

Here is the error message:

Screenshot from 2024-05-18 16-07-39

If I am misunderstanding something, please let me know.

from channels.

carltongibson avatar carltongibson commented on August 24, 2024

It looks like you've overridden a method maybe and forgotten an async.

Please post code rather than screenshots.

The async send_json method is covered by the test suite. If you could add a test demonstrating your issue, that would help to identify what you're seeing.

from channels.

gaurav-jo1 avatar gaurav-jo1 commented on August 24, 2024

chats/consumers.py

class ChatConsumer(AsyncJsonWebsocketConsumer):
    async def connect(self):
        # Accept the connection
        await self.accept()

        # Send a welcome message
        await self.send_json(
            {
                "type": "welcome_message",
                "message": "Welcome to the Websocket Connection",
            }
        )

The error traceback is as follows:

python3.12/site-packages/channels/generic/websocket.py", line 282, in send_json
    await super().send(text_data=await self.encode_json(content), close=close)
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: object str can't be used in 'await' expression

The full chats/consumers.py file can be found here: ChatConsumer file.

Here is a video demo of the error.

Screencast.from.2024-05-20.17-22-47.webm

from channels.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.