WebSocket + Django python WebService [英] WebSocket + Django python WebService

查看:478
本文介绍了WebSocket + Django python WebService的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何用websockets创建一个django webservice(用XML响应)。
我已经有一个django webservice接受xml请求,解析这些请求,进行数据库查询,创建一个响应xml并将该XML发送回请求者/浏览器。只是一个正常的HTTP XML请求,其中响应在浏览器中显示为xml。

I was wondering how to create a django webservice (responds with XML) with websockets. I have already a django webservice which accepts xml requests, parse those requests, makes a database query, creates a response xml and send that xml back to the requester/browser. Just a normal HTTP XML request, where the response is shown as xml within the browser.

但是,我现在如何创建一个websocket django webservice?假设我想在发生新的神奇事件时,向请求者/浏览器发送一个XML数据库中的最新数据的xml响应。

But how would i now create a websocket django webservice? Lets say i would like to send a xml response to the requester/browser with the latest data from database whenever a new magical event occurs.

我已经阅读了很多帖子和博客,但这样做太笼统了。我可以使用django + apache解决这个问题,还是需要django旁边的其他服务器,另外一个服务器只能处理websockets?

I have read a lot of posts and blogs but it was kinda all too general. Can i solve this only with django + apache or do i need something else next to django and another server only to handle websockets?

我现在正在使用django 1.3,Apache + wsgi,但我可以随时切换任何可以正常工作的配置。

I am right now using django 1.3, Apache + wsgi but i would be ready to switch any configuration that would work.

更新:

有很多可能的websockets,
http://pypi.python.org/pypi?:action=search&term=websocket&submit=search
,但在我的情况下可以使用哪一个?

There are many possible websockets out there, http://pypi.python.org/pypi?:action=search&term=websocket&submit=search but which one could be used in my case?

推荐答案

对不起,django处理异步请求非常糟糕,因为它是wsgi。如果您需要处理真实的用户,您将受到并行实例数的限制。最好的解决方案是使用龙卷风或node.js。

Sorry but django handles async requests very very poorly as it is wsgi. You'll be limited by your number of parallel instance if you have to handle real users. The best solution is to use tornado or node.js.

龙卷风处理websocket和长时间的投票。这是我的包装器,允许从并行龙卷风线程获取用户和会话:

Tornado handles websocket and long polling brilliantly. Here is my wrapper to allow getting user and sessions from a parallel tornado thread:

https://gist.github.com/1939836

它来自一个更复杂的来源,我没有测试这个要点,它是漫长的轮询,但龙卷风手提WebSocket。

It's adapted from a more complex source, I didn't tested this gist, it's long polling but tornado handlse WebSocket as well.

http://www.tornadoweb.org/documentation/websocket.html

更新:

避免使用django-websocket进行生产。即使是主要的开发人员建议反对它。

Avoid django-websocket for production use. Even the main developer recommends against it.

我推荐龙卷风,因为它是一个令人敬畏的技术,比django更快/更轻。对于一些简单的情况可能是有用的。您需要配置apache / nginx,所以至少得到更快的网页功能。

I recommend Tornado because it's an awesome technology which is damningly faster/lighter than django. It may be useful for some simple cases. You'll need to configure apache/nginx anyway so, at least get "faster web pages" feature available.

Django-Desktop-Notification专注于Chrome浏览器和要求节点.js。

Django-Desktop-Notification focuses on chrome browser and require node.js.

更新(01/2016):

Mozilla给了钱到django在2015年底解决这个特定的问题,目前最有希望的一个django核心开发实现是这样的:

Mozilla gave money to django in late 2015 to solve this particular issue, the current most promizing implementation made by a django core dev is this one:

https://github.com/andrewgodwin/channels

它可能是django 1.11或2.0

It will probably be part of django 1.11 or 2.0

这篇关于WebSocket + Django python WebService的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆