GithubHelp home page GithubHelp logo

Comments (9)

aaugustin avatar aaugustin commented on September 27, 2024

This is probably a good idea, however, it's a breaking API change: ws.send() would become a coroutine, while it's currently a function.

For the record, drain() was added to asyncio recently. It didn't exist when I wrote most of the current implementation.

from websockets.

chrisprobst avatar chrisprobst commented on September 27, 2024

Not necessarily, it should be possible to simply add a drain()-like method to the exposed API.

It's like the asyncio API. If you do heavy streaming, you should yield from drain() after sending. If you are only transferring a bit of json, well, do not care.

Because the current implementation does not use StreamWriter, this functionality should be implemented using a future and the following callbacks:

BaseProtocol.pause_writing()
Called when the transport’s buffer goes over the high-water mark.

BaseProtocol.resume_writing()
Called when the transport’s buffer drains below the low-water mark.

This functionality is critical for every bandwidth-hungry websocket application.

from websockets.

aaugustin avatar aaugustin commented on September 27, 2024

I'd rather have the library handle this behind the scenes. It's focused on simplicity.

I don't want to inflict flow control on my users and certainly not with a low-level API.

The point of this library is to provide a high-level API that "just works" -- even if it's not the fastest websockets library on Earth.

Changing the API just means I have to release a 2.0. There's nothing wrong with that.

from websockets.

chrisprobst avatar chrisprobst commented on September 27, 2024

You are right, it would be simpler and better. Version 2.0 sounds good, definitely a huge improvement for streaming apps (I'm working on a terrain streaming app with high bandwidth requirements).

from websockets.

aaugustin avatar aaugustin commented on September 27, 2024

I'm currently working on making WebSocketCommonProtocol inherit from StreamReaderProtocol.

This convenience API was also added recently and appears suitable for this use case.

That will make WebSocketCommonProtocol use StreamWriter and make flow control possible through private (undocumented) attributes -- ie. you will be able to yield from ws.writer.drain(). This workaround will allow you to move forward while I think about a more general solution.

from websockets.

chrisprobst avatar chrisprobst commented on September 27, 2024

=) Awesome. That will do it.

from websockets.

aaugustin avatar aaugustin commented on September 27, 2024

In my pull request, the protocol uses a StreamWriter. If you can try it and confirm that yield from ws.writer.drain() does what you expect, that would be great.

from websockets.

chrisprobst avatar chrisprobst commented on September 27, 2024

I can confirm that it does work great. Thanks!

from websockets.

aaugustin avatar aaugustin commented on September 27, 2024

Cool. I'm going to merge the patch now.

I think I'll wait until Python 3.4 final is released to make a new release of websockets, in case asyncio changes again. Hopefully you can work with the master until then.

from websockets.

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.