flask stream does not work
fenol64
PROOP

a month ago

I hava a API that has an andpoint where I submit two files, on localhost works on PROD (google cloud) works but on railway the connection just dies

does not give an error log, nothing

just give a 200 with the first line returned with yield

curl command by swagger:

curl -X 'POST' \ 'https://bez-stat-api-dev.up.railway.app/api/v1/analysis/objectives/ai-stream' \

-H 'accept: application/json' \

-H 'Authorization: TOKEN' \

-H 'Content-Type: multipart/form-data' \

-F 'database=@bd.xlsx;type=application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' \

-F 'objectives=@objetivos.pdf;type=application/pdf'

RESPONSE:

https://bez-stat-api-dev.up.railway.app/api/v1/analysis/objectives/ai-stream

Request Method

POST

Status Code

200 OK

yield "[DATA] info: Iniciando análise...\n\n"

Solved$20 Bounty

Pinned Solution

fenol64
PROOP

a month ago

I resolved using flask_sse but IDK why it does not work with stream_with_context function

17 Replies

Railway
BOT

a month ago

This thread has been marked as public for community involvement, as it does not contain any sensitive or personal information. Any further activity in this thread will be visible to everyone.

Status changed to Open Railway about 1 month ago


I'm able to POST to the URL just fine. I'm not too sure what the issue exactly is, considering you mentioned that the connection dies.

$ curl -X POST https://bez-stat-api-dev.up.railway.app/api/v1/analysis/objectives/ai-stream
{"message": "Token n\u00e3o fornecido"}

0x5b62656e5d

I'm able to POST to the URL just fine. I'm not too sure what the issue exactly is, considering you mentioned that the connection dies. ``` $ curl -X POST https://bez-stat-api-dev.up.railway.app/api/v1/analysis/objectives/ai-stream {"message": "Token n\u00e3o fornecido"} ```

fenol64
PROOP

a month ago

curl -X 'POST' \ 'https://bez-stat-api-dev.up.railway.app/api/v1/analysis/objectives/ai-stream' \

-H 'accept: application/json' \

-H 'Authorization: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiNjljYzdmNzYxZDFkOTlhNmQ2NDZlYWE2IiwiZXhwIjoxODA4MjcxMDMyfQ.8xz4WZaIIEkJ13UaE976AnUWJuybeYTSSPXtPyxrHz8' \

-H 'Content-Type: multipart/form-data' \

-F 'database=@bd.xlsx;type=application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' \

-F 'objectives=@objetivos.pdf;type=application/pdf'


fenol64

curl -X 'POST' \\ '<https://bez-stat-api-dev.up.railway.app/api/v1/analysis/objectives/ai-stream>' \\ \-H 'accept: application/json' \\ \-H 'Authorization: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiNjljYzdmNzYxZDFkOTlhNmQ2NDZlYWE2IiwiZXhwIjoxODA4MjcxMDMyfQ.8xz4WZaIIEkJ13UaE976AnUWJuybeYTSSPXtPyxrHz8' \\ \-H 'Content-Type: multipart/form-data' \\ \-F 'database=@bd.xlsx;type=application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' \\ \-F 'objectives=@objetivos.pdf;type=application/pdf'

fenol64
PROOP

a month ago

emulated token for you to test it


fenol64

emulated token for you to test it

fenol64
PROOP

a month ago

the database needs to be a .xlsx or csv file and the objectives is a pdf


fenol64

the database needs to be a .xlsx or csv file and the objectives is a pdf

fenol64
PROOP

a month ago

the URL is OK, but when it starts the streaming the connection dies and just returns the first line returned

my api is in python so I have a function that use 'yield' as return

yield "[DATA] info: Iniciando análise...\n\n"

yield "[DATA] A\n\n"

yield "[DATA] bn\n"

it just returns the first one yield "[DATA] info: Iniciando análise...\n\n" and the connection dies


fenol64
PROOP

a month ago

this is how im using

        return Response(
            stream_with_context(self.generate_ai_objectives_stream()),
            mimetype='text/event-stream',
            headers={
                'Cache-Control': 'no-cache',
                'Connection': 'keep-alive',
                'X-Accel-Buffering': 'no'
            }
        )

You need to yield data: instead of [DATA].

For example:

yield "data: Some text stream"


0x5b62656e5d

You can also reference this: <https://utilities-us-east.up.railway.app/sse>

fenol64
PROOP

a month ago

I edited my code but does not work

same thing:

data: Iniciando análise...


fenol64

I edited my code but does not work same thing: data: Iniciando análise...

fenol64
PROOP

a month ago

it looks like the connection is just not keeping alive


fenol64
PROOP

a month ago

tcp

35.232.42.139:27017

10.217.122.222:56450

Internet

5.5 KB

34ms

NO_SOCKET

just found this log on 'Network Flow Logs'


I'd try putting print/log statements before/after each yield so you can debug where your code (possibly) crashed in deployment logs.


fenol64
PROOP

a month ago

just worked on my insominia, but i clicked again did not work


fenol64
PROOP

a month ago

i dont think this is a code error, i think its more a keep the connection to the client


Sorry but not sure how to go about this one.

Railway's SSE sample works perfectly fine. (https://utilities-us-east.up.railway.app/sse)


fenol64
PROOP

a month ago

u have the repo?


fenol64
PROOP

a month ago

I resolved using flask_sse but IDK why it does not work with stream_with_context function


Status changed to Solved brody about 1 month ago


Welcome!

Sign in to your Railway account to join the conversation.

Loading...