librelist archives

« back to archive

Long Polling with Flask on Heroku

Long Polling with Flask on Heroku

From:
Colin L Rice
Date:
2011-12-05 @ 16:34
Hello,

I was trying to write a long polling application and need to push a byte 
down a http connection every 30 seconds or so. I was wondering if anyone 
has done something similar or if there is a easy way to do it within flask?

-Colin

Re: [flask] Long Polling with Flask on Heroku

From:
Armin Ronacher
Date:
2011-12-06 @ 05:13
Hi,

On 12/5/11 11:34 AM, Colin L Rice wrote:
> I was trying to write a long polling application and need to push a byte 
> down a http connection every 30 seconds or so. I was wondering if anyone 
> has done something similar or if there is a easy way to do it within flask?
Hosted solutions like beaconpush.com or pusher.com exist.  If you want
to host this yourself there is juggernaut:
http://flask.pocoo.org/snippets/80/

IMO all of the solutions above beat any kind of hackery you can do yourself.


Regards,
Armin

Re: [flask] Long Polling with Flask on Heroku

From:
Simon Sapin
Date:
2011-12-05 @ 17:00
Le 05/12/2011 17:34, Colin L Rice a écrit :
> Hello,
>
> I was trying to write a long polling application and need to push a byte
> down a http connection every 30 seconds or so. I was wondering if anyone
> has done something similar or if there is a easy way to do it within flask?
>
> -Colin

Hi,

The idea of long polling is to accept an HTTP request, block, and only 
send a response later. When the client gets a response, it immediately 
sends a new request that will block, etc.

It is very easy to implement, but each blocked request (about one per 
simultaneous client) uses a worker thread/process/whatever. This can be 
expensive on Heroku as I guess they bill for clock time and not CPU time.

However, long polling is interesting if the client doesn’t know when the 
next event/response will come. In your case if the timing is fixed, it 
may be easier to simply have the client send a normal request every 30 
seconds.

Regards,
-- 
Simon Sapin

Re: [flask] Long Polling with Flask on Heroku

From:
Colin L Rice
Date:
2011-12-05 @ 17:06
On 12/05/2011 12:00 PM, Simon Sapin wrote:
> Hi,
>
> The idea of long polling is to accept an HTTP request, block, and only
> send a response later. When the client gets a response, it immediately
> sends a new request that will block, etc.
>
> It is very easy to implement, but each blocked request (about one per
> simultaneous client) uses a worker thread/process/whatever. This can be
> expensive on Heroku as I guess they bill for clock time and not CPU time.
What I currently have is that sort of long polling method. However due 
to limitations on the heroku side they automatically close all 
connections after 30 seconds. In order to support long polling they 
require that you send back a byte every 30 seconds to keep the socket 
open until you can return what you actually want to return.

-Colin

Re: [flask] Long Polling with Flask on Heroku

From:
kracekumar ramaraju
Date:
2011-12-05 @ 18:54
Hi

On Mon, Dec 5, 2011 at 10:36 PM, Colin L Rice <ricec2@rpi.edu> wrote:

> On 12/05/2011 12:00 PM, Simon Sapin wrote:
> > Hi,
> >
> > The idea of long polling is to accept an HTTP request, block, and only
> > send a response later. When the client gets a response, it immediately
> > sends a new request that will block, etc.
> >
> > It is very easy to implement, but each blocked request (about one per
> > simultaneous client) uses a worker thread/process/whatever. This can be
> > expensive on Heroku as I guess they bill for clock time and not CPU time.
> What I currently have is that sort of long polling method. However due
> to limitations on the heroku side they automatically close all
> connections after 30 seconds. In order to support long polling they
> require that you send back a byte every 30 seconds to keep the socket
> open until you can return what you actually want to return.
>
> I might be wrong. I think you should try using redis or websockets here

-Colin
>



-- 
*
"Talk is cheap, show me the code" - Linus Torvalds
Winning Regards
KraceKumar.R
http://kracekumar.wordpress.com
+91-97906-58304
*
*+91-85530-29521*
*
*

Re: [flask] Long Polling with Flask on Heroku

From:
Joe Esposito
Date:
2011-12-05 @ 19:04
I don't think WSGI has any websocket-like support; I kept running into
issues when trying to use them within Flask.
It looks like PEP 444 is revising WSGI for Python 3, and I'm hoping that
helps make websockets more able to be integrated into a web application.

Does anyone else have a different experience with this?

On Mon, Dec 5, 2011 at 1:54 PM, kracekumar ramaraju <
kracethekingmaker@gmail.com> wrote:

> Hi
>
> On Mon, Dec 5, 2011 at 10:36 PM, Colin L Rice <ricec2@rpi.edu> wrote:
>
>> On 12/05/2011 12:00 PM, Simon Sapin wrote:
>> > Hi,
>> >
>> > The idea of long polling is to accept an HTTP request, block, and only
>> > send a response later. When the client gets a response, it immediately
>> > sends a new request that will block, etc.
>> >
>> > It is very easy to implement, but each blocked request (about one per
>> > simultaneous client) uses a worker thread/process/whatever. This can be
>> > expensive on Heroku as I guess they bill for clock time and not CPU
>> time.
>> What I currently have is that sort of long polling method. However due
>> to limitations on the heroku side they automatically close all
>> connections after 30 seconds. In order to support long polling they
>> require that you send back a byte every 30 seconds to keep the socket
>> open until you can return what you actually want to return.
>>
>> I might be wrong. I think you should try using redis or websockets here
>
>  -Colin
>>
>
>
>
> --
> *
> "Talk is cheap, show me the code" - Linus Torvalds
> Winning Regards
> KraceKumar.R
> http://kracekumar.wordpress.com
> +91-97906-58304
> *
> *+91-85530-29521*
> *
> *
>
>

Re: [flask] Long Polling with Flask on Heroku

From:
Simon Sapin
Date:
2011-12-05 @ 20:32
Le 05/12/2011 18:06, Colin L Rice a écrit :
> What I currently have is that sort of long polling method. However due
> to limitations on the heroku side they automatically close all
> connections after 30 seconds. In order to support long polling they
> require that you send back a byte every 30 seconds to keep the socket
> open until you can return what you actually want to return.

See the Flask doc on how to stream content on the response: (ie. send it 
on the network part-by-part. By default stuff like render_template() 
only return when they’re done.)

http://flask.pocoo.org/docs/patterns/streaming/

You need to have a 'yield' every 20-something seconds.
To do that it depends on how you block, what you’re waiting for. Some 
operations (network calls, …) have a timeout parameter.

Regards,
-- 
Simon Sapin

Re: [flask] Long Polling with Flask on Heroku

From:
Colin L Rice
Date:
2011-12-05 @ 20:38
On 12/05/2011 03:32 PM, Simon Sapin wrote:
> Le 05/12/2011 18:06, Colin L Rice a écrit :
>> What I currently have is that sort of long polling method. However due
>> to limitations on the heroku side they automatically close all
>> connections after 30 seconds. In order to support long polling they
>> require that you send back a byte every 30 seconds to keep the socket
>> open until you can return what you actually want to return.
> See the Flask doc on how to stream content on the response: (ie. send it
> on the network part-by-part. By default stuff like render_template()
> only return when they’re done.)
>
> http://flask.pocoo.org/docs/patterns/streaming/
>
> You need to have a 'yield' every 20-something seconds.
> To do that it depends on how you block, what you’re waiting for. Some
> operations (network calls, …) have a timeout parameter.
>
> Regards,
Thats exactly what I was looking for. Thank you.

-Colin