Code Monkey home page Code Monkey logo

Comments (6)

mr-flannery avatar mr-flannery commented on September 22, 2024 1

Hi @Ruwann ,

thanks for the quick response! Your suggestion did solve the problem!

from starlette.responses import StreamingResponse

# ...

async def streaming():
    return StreamingResponse(async_gen_numbers(), status_code=200, media_type='text/plain')

from connexion.

Ruwann avatar Ruwann commented on September 22, 2024

Hi @mr-flannery ,
I'll have to look into the is_streamed parameter, it seems that it is not correctly used.

In the meantime, can you try by using the underlying Starlette StreamingResponse?

from connexion.

mr-flannery avatar mr-flannery commented on September 22, 2024

Hi @Ruwann ,

unfortunately, my response was a bit premature. It looks like it doesn't actually work as expected. While the code does run, it does not actually stream. From a caller's perspective it's still synchronous.

When calling the endpoint with curl, it still blocks for 10 seconds before anything happens, i.e.:

$ curl -N http://localhost:8080/streaming
# nothing happens for 10 seconds
0123456789
$

I wrote an Express app to make sure this was actually the server's fault, not curls:

const Express = require('express');
const app = new Express();

async function* generateNumbers() {
  for (const i of [1, 2, 3, 4, 5]) {
    await new Promise(resolve => setTimeout(resolve, 1000));
    yield await Promise.resolve(i);
  }
}

app.get('/streaming', async (req, res) => {
  for await (const i of generateNumbers()) {
    res.write(`data: ${i}\n\n`);
  }
  res.end();
});

app.listen(3456, () => {
  console.log('Server is running on port 3456');
})

When using curl here, this works as expected, i.e. it sends a chunk of data every second.

Therefore, I believe there might actually be a bug with sending StreamingResponses in connexion3 + AsyncApp.

from connexion.

Ruwann avatar Ruwann commented on September 22, 2024

I cannot seem to reproduce the issue. When I use StreamingResponse from starlette, the response is streamed to my terminal as expected.

I used the following sample code: https://github.com/Ruwann/connexion-streaming

from connexion.

mr-flannery avatar mr-flannery commented on September 22, 2024

Hi @Ruwann ,

thanks for the repo, I was able to track it down. This is what causes the behavior to change:

app.add_api("openapi.yaml", validate_responses=True)

Once I add validate_responses=True to your code, it exhibits the same problem. Vice versa, if I remove it from my code, streaming works as expected.

from connexion.

mr-flannery avatar mr-flannery commented on September 22, 2024

Hi @Ruwann ,

just wanted to check in with you and see if there are there any plans to fix this?

Best regards!

from connexion.

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.