using gevent and websockets in a traditional web app

57 views
Skip to first unread message

Peter Baumgartner

unread,
Nov 13, 2010, 5:28:48 PM11/13/10
to gevent: coroutine-based Python network library
Hi, I'm new to gevent and to event-based programming in general. I'm
building a traditional web application with one page that uses
websockets to stream data (1-way) to the user via geventwebsocket. The
websocket needs to send different data depending on the logged-in
user.

I'd like to build the bulk of the application in Django with a
Postgres backend. Given the very different requirements of the
websocket bits (non-blocking code and server), would it make sense to
have a separate lightweight module using something like Flask for
handling websockets? If so, how do I signal it from an external
process to send new data? I hacked something together so the websocket
portion can poll memcache for new data, but it seems like there must
be a better way.

Thanks in advance for any advice on how to put all the pieces
together!

-- Pete

Jeffrey Gelens

unread,
Nov 16, 2010, 4:19:36 AM11/16/10
to gevent: coroutine-based Python network library
Hi Peter,

Are you running the Django project using Gevent or for example
Gunicorn with Gevent workers?
If so, you could run the Websocket internally and use the Gevent Event
or Queue functionality to signal the Websocket to send some data.

Otherwise, at the moment, I don't see an other way than to let a
lightweight Gevent app poll some resource for information.

--
Jeffrey Gelens

Peter Baumgartner

unread,
Nov 16, 2010, 10:28:32 AM11/16/10
to gev...@googlegroups.com
On Tue, Nov 16, 2010 at 2:19 AM, Jeffrey Gelens <jge...@gmail.com> wrote:
> Hi Peter,
>
> Are you running the Django project using Gevent or for example
> Gunicorn with Gevent workers?

I could, but I guess I thought you couldn't just plug gevent into your
Django project and have it "work." What I don't understand is...

1. Do I need to make sure all the requests are non-blocking or just
the websocket stuff?
2. AFAIK, Nginx can't handle websockets and Gunicorn isn't recommended
without a server like Nginx in front of it.

Jeffrey Gelens

unread,
Nov 16, 2010, 4:20:37 PM11/16/10
to gevent: coroutine-based Python network library
On Nov 16, 4:28 pm, Peter Baumgartner <sgt.hu...@gmail.com> wrote:
> 1. Do I need to make sure all the requests are non-blocking or just
> the websocket stuff?
Only the Websocket can be non-blocking as it will run separately from
your Django application as I understand from you description.

> 2. AFAIK, Nginx can't handle websockets and Gunicorn isn't recommended
> without a server like Nginx in front of it.
If you use Gevent or Gunicorn for only Websockets strictly I don't see
any problems. It's at the moment the only way to use Websockets. I
didn't find any webserver which can proxy sockets correctly.

You can also contact me on IRC (Freenode) with the nick jgelens.
Reply all
Reply to author
Forward
0 new messages