Giter Club home page Giter Club logo

Comments (4)

pgjones avatar pgjones commented on July 18, 2024

I think splitting this into producing and consuming tasks makes sense, although I'm not sure. Does this work and make sense to you? I think it is the right idea.

@copy_current_websocket_context
async def send():
    while True:
        data = await aioredis.receive()
        await websocket.send(data)

@copy_current_websocket_context
async def receive():
    while True:
        data = await websocket.receive()
        await aioredis.send(data)

@app.websocket('/')
async def index():
    sender = asyncio.ensure_future(send())
    receiver = asyncio.ensure_future(receive())
    gathered = asyncio.gather(sender, receiver)
    try:
        await gathered
    except CancelledError:  # Is raised when websocket connection closes
        gathered.cancel()

from quart.

juntiedt2 avatar juntiedt2 commented on July 18, 2024

from quart.

juntiedt2 avatar juntiedt2 commented on July 18, 2024

I tried the following code together with your example Quart echo web page.
Receiving data from redis queue and sending it to the webpage works ok. But receiving data from the webpage does not work. Why?

from quart import Quart, render_template, websocket
from RedisQueue import RedisQueue

app = Quart(__name__)
q_in = RedisQueue('ws_2')
q_out = RedisQueue('ws_3')

async def aioredis_receive():
    #return q_in.get()
    return q_in.get().decode("utf-8")
    
async def aioredis_send(data):
    q_out.put(data)    

@app.route('/')
async def index():
    return await render_template('index.html')

@app.websocket('/ws')
async def send():                        #send to Web-Page
    while True:
        data = await aioredis_receive()
        print("From redis queue: ", data)
        await websocket.send(data)

@app.websocket('/ws')
async def receive():                     #receive from Web-Page
    while True:
        data = await websocket.receive()
        print("From Web-Page: ", data)
        await aioredis_send(data)

@app.websocket('/ws')
async def ws():
    sender = asyncio.ensure_future(send())
    receiver = asyncio.ensure_future(receive())
    gathered = asyncio.gather(sender, receiver)
    try:
        await gathered
    except CancelledError:  # Is raised when websocket connection closes
        gathered.cancel()
        
if __name__ == '__main__':
    app.run(port=5000)

from quart.

pgjones avatar pgjones commented on July 18, 2024

I don't think redis queue is based on asyncio, so it will likely block the event loop. It may work to switch to use asyncio.run_in_executor but I think asyncio-redis is a better choice.

I'm also ensure what happens if you define a route many times (@app.websocket('/ws')), I don't think you need to and I think the last call will overwrite the previous ones only the first one counts.

from quart.

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.