websocket内存管理

发布于 2025-01-17 05:40:54 字数 1054 浏览 5 评论 0原文

我是 websockets 的新手,正在通过 Coinbase websocket 提取一些数据,在每条消息中我都会将其附加到内存中的 pandas df 中(我知道这是不好的做法,只是想获得一个工作版本)。我每分钟都会将此 df 上传到 TimescaleDB 并清除 df 中的旧数据。

我注意到,有时我无法上传 df,因此我没有清除 df 的旧值,最终 df 消耗了所有内存。

这是websocket的一个特性吗?或者我的调度程序出问题了?

这是我的调度程序供参考 -

while True
       nowtime = datetime.now()
        floornow = pd.Timestamp(nowtime).floor(freq='1S')
        candlefloor = floornow.floor(freq=f'{candle_len}T')
        if (floornow == candlefloor):
            try: 
                upload_timescale()

                if (candlefloor != 'last datetime'):
                    timetowait = candle_len*60-(datetime.now() - candlefloor).total_seconds()-0.05
                    time.sleep(timetowait)

            except: 
                raise ValueError('bug in uploading to timescaledb')

         
        else:
            tts = candle_len*60-(nowtime - candlefloor).total_seconds() 
            if (tts > 2):
                time.sleep(tts)

在清理之前,我需要间歇性地存储 websocket 数据以进行处理,处理该用例的最佳方法是什么?

谢谢!

I am new to websockets and am pulling some data via the Coinbase websocket, upon every message I append to a pandas df in memory (I know this is bad practice, just trying to get a working version). Every minute I upload this df to TimescaleDB and clear out old data from the df.

I am noticing though that on occasion I am failing to upload the df and as a result I am not clearing the df of old values, and eventually the df consumes all the memory.

Is this a feature of websockets? Or is something off with my scheduler?

This is my scheduler for reference -

while True
       nowtime = datetime.now()
        floornow = pd.Timestamp(nowtime).floor(freq='1S')
        candlefloor = floornow.floor(freq=f'{candle_len}T')
        if (floornow == candlefloor):
            try: 
                upload_timescale()

                if (candlefloor != 'last datetime'):
                    timetowait = candle_len*60-(datetime.now() - candlefloor).total_seconds()-0.05
                    time.sleep(timetowait)

            except: 
                raise ValueError('bug in uploading to timescaledb')

         
        else:
            tts = candle_len*60-(nowtime - candlefloor).total_seconds() 
            if (tts > 2):
                time.sleep(tts)

What is the best way to handle a use case where I need to store websocket data intermittently to process, before cleaning it out?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文