简体   繁体   English

使用 django-channels/websocket 更新实时位置的有效方法

[英]Efficient way of updating real time location using django-channels/websocket

I am working on Real Time based app, it needs to update location of user whenever it is changed.我正在开发基于实时的应用程序,它需要在更改时更新用户的位置。

Android app is used as frontend, which get location using Google/Fused Api and in onLocationChanged(loc:Location) , I am sending the latest location over the Websocket . Android 应用程序用作前端,它使用Google/Fused Api获取位置,在onLocationChanged(loc:Location) ,我通过Websocket发送最新位置。 The location update is then received by a django channel consumer , and job of this consumer is to store location in database asynchronously (I am using @database_sync_to_async decorator.然后位置更新由django 通道消费者接收,该消费者的工作是将位置异步存储在数据库中(我正在使用@database_sync_to_async装饰器。

But the problem is, server crashes when Android app tries to send 10-15 location updates per second.但问题是,当 Android 应用尝试每秒发送 10-15 个位置更新时,服务器会崩溃。 What will be the efficient way of updating real time location?更新实时位置的有效方法是什么?

Note: Code can be supplied on demand注:代码可按需提供

Ask yourself what kind of resolution you need for that data.问问自己需要什么样的分辨率来处理这些数据。 Do you really need 10 updates a second?你真的需要每秒更新 10 次吗? If not, take every nth update or see if Android will just give you the updates slower.如果没有,请每第 n 次更新,或者看看 Android 是否只会给你更慢的更新。 Secondly, look for a native async database library.其次,寻找本机异步数据库库。 @database_sync_to_async runs a different thread every time you call it which kills the performance gains you're getting from the event loop. @database_sync_to_async 每次调用时都会运行一个不同的线程,这会扼杀您从事件循环中获得的性能提升。 If you say in one thread you'll keep the CPU caches fresh.如果您在一个线程中说,您将使 CPU 缓存保持新鲜。 You won't get to use the ORM.您将无法使用 ORM。 But do you really need a database or would Redis work?但是你真的需要一个数据库还是 Redis 可以工作? If so, call aioredis directly and it will be a lot faster since its in memory and you can use it's fast data structures like queues and sets.如果是这样,直接调用 aioredis 会快很多,因为它在内存中,你可以使用它的快速数据结构,如队列和集合。 If you need Redis to be even faster look at it's multithreaded fork KeyDB.如果您需要 Redis 更快,请查看它的多线程分支 KeyDB。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM