Comments (4)
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.
from quart.
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.
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)
- async function support in url_defaults and url_value_preprocessor HOT 1
- incorrect display img logo on https://quart.palletsprojects.com/en/latest/ HOT 1
- Remove poetry from examples in documentation HOT 2
- Allow kwargs in Quart.test_client for custom test client implementations HOT 1
- unix socket support HOT 1
- reload() puts comman name after command line arguments HOT 2
- Hot reload does not work within poetry scripts HOT 2
- New doc contribution: Add a doc to integrate Quart with BentoML HOT 1
- The parameter of ContentRange in quart.wrappers.Response._process_range_request is not correct.
- Handling CTRL+C for websockets [linux] HOT 2
- 'quart.app' vs 'app' for log level configuration via dictConfig
- [Docs] AttributeError: 'async_generator' object has no attribute 'test_client'
- after_this_websocket is not called if the client disconnects?
- Background tasks can disappear mid-execution HOT 1
- Server sent events guide: Add = None to optional retry argument
- Cannot get request files with NGINX HOT 1
- Hot-reload not working on paths with spaces
- Blog tutorial has typo for path
- Support Accept-Range header in send_file?
- Hot reload cause a permission denied error in docker container HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from quart.