如何在websocket数据中快速接收响应的过程? [英] How to apply process on very fast receiving response in websocket data ?

查看:102
本文介绍了如何在websocket数据中快速接收响应的过程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过网络套接字与股票市场实时数据提供商建立联系。为了响应所有公司的实时价格,我每分钟获得近40,000条记录。我需要将所有数据存储到文本文件中,以便我需要为所有响应应用我的代码
,当我为所有响应编写代码时,它会使进程非常慢。当我在响应上应用代码时,它只执行5,000条记录。所以与我们的回答相比,这是非常缓慢的。那么如何让我们的流程快速完成,这样我就能以接收速度存储数据
。请帮我解决这个问题。

Hi, I am making connection with stock market live data provider through web sockets. I am getting nearly 40,000 records per minute in response for all companies live price. I need to store all that data into text file and so that I need to apply my code for all responses and when I write code for all responses then it makes process very slow. When I apply code on response then it execute only 5,000 records. So it's very slow as compare to our responses. So how to make our process fast so I can store data with receiving speed. Please help me to solve this.

我在下面写了我的代码:

I have write my code below :

推荐答案

好的,一些推荐更快:

Okay, some recommendations to make it faster:

首先,避免临时文件。每次收到消息时,都会将其保存到临时文件,然后打开逐行读取它的流读取器的临时文件。

First, avoid temporary files. Every time you receive a message, you are saving it to a temporary file and then opening the temporary file for a streamreader that reads it line by line.

相反,打开一个StringReader反对收到的消息信息。然后,您可以从读取StreamReader的相同方式读取StringReader。但是你保存了中间文件;一切都在内存中完成。

Instead, open a StringReader against the received message. Then you can read from the StringReader in the same way as you were reading from the StreamReader. But you save the intermediate file; everything is done in memory.

然后,在DumpLog中,您使用AppendText打开一个文件。打开要追加的文件是一个缓慢的操作。而是打开文件一次并保持打开状态。然后,只需继续写入文件。这将导致每个写入在末尾附加,
而无需打开并重新打开该文件。更重要的是,你现在使用该文件的方式,你只需关闭它并阅读它。躲开它。不要使用该文件。只需应用与开头相同的原则,并在不使用临时文件的情况下继续读取内存
中的上一步骤。完成所有处理后,只在最后写入文件。

And then, in DumpLog you are opening a file using AppendText. Opening a file for appending is a slow operation. Instead, open the file once and keep it open. Then, simply keep writing into the file. This will result in each write being appended at the end, without having to open and reopen the file. Even more, the way you are using the file now, you just close it and read it back. Avoid it. Don't use the file. Simply apply the same principle as in the beginning, and keep reading from the preceding step in memory without using temporary file. Only write into a file at the very end, when all processing is done.

如果不使用中间文件将所有内容保存在内存中,并保持目标文件打开而不关闭并重新打开,那么每分钟40.000条记录应该很容易实现。即使每秒40,000也应该没有困难。

If you keep everything in memory without using intermediate files, and you keep the destination file open without closing and reopening, then 40.000 records per minute should be easy to achieve. Even 40.000 per second should be doable without difficulty.


这篇关于如何在websocket数据中快速接收响应的过程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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