首页 新闻 会员 周边 捐助

怎么实时处理大量数据

0
悬赏园豆:50 [已关闭问题] 解决于 2015-12-08 13:34

是这样的,目前我在做一个实时股票行情的接口,实时行情通过一个行情接收器接收实时数据,然后保存在本地文件中,也就是说,实时行情是通过这个接收器实时更新文件的。

 

服务器端,我去定时读取这个实时文件,然后生成1分钟、5分钟、15分钟、周、月、年的k线,然后再生成技术指标。

 

目前股票大约4000多只,每只有一个k线+7个指标,k线和指标分别对应9个周期,发现实时处理延迟很厉害,并且需要先生成k线,才能生成指标,而且周期之间也有先后顺序,5分钟依赖一分钟,30分钟以来15分钟,周、月、年依赖日线。

 

目前的方案是,我先定义了8个线程,每个线程里面

将实时处理定义成一个委托,参数和委托封装成一个类WorkItem
将实时生成的操作放到并行队列,利用一个新的线程去检测队列并处理操作
如果队列有数据,就执行里面的委托
 
但是这样的话顺序无法保证,请问有什么好的处理方法,非常感谢
 
注:由于是遗留代码,是用VB .NET写的
部分代码
    Dim queueMACD As New ConcurrentQueue(Of WorkItemIndex)
    Dim queueKDJ As New ConcurrentQueue(Of WorkItemIndex)
    Dim queueRSI As New ConcurrentQueue(Of WorkItemIndex)
    Dim queueCCI As New ConcurrentQueue(Of WorkItemIndex)
    Dim queueWR As New ConcurrentQueue(Of WorkItemIndex)
    Dim queueBOLL As New ConcurrentQueue(Of WorkItemIndex)
    Dim queueBIAS As New ConcurrentQueue(Of WorkItemIndex)
    Sub ThMakeMACDReal()
        Do
            If queueMACD.Count > 0 Then
                Dim workItem As WorkItemIndex
                Dim result = queueMACD.TryDequeue(workItem)
                If result = True Then
                    Me.Invoke(New Action(Sub()
                                             lblMACD.Text = queueMACD.Count
                                         End Sub))
                    log.Debug("MACD队列剩余" & queueMACD.Count & "")
                    workItem.DelegateMakeIndexRt.Invoke(workItem.Args(0))
                End If
            End If
            Thread.Sleep(1000)
        Loop Until bStopDoRealThread = True
    End Sub

    Sub ThMakeKDJReal()
        Do
            If queueKDJ.Count > 0 Then
                Dim workItem As WorkItemIndex
                Dim result = queueKDJ.TryDequeue(workItem)
                If result = True Then
                    Me.Invoke(New Action(Sub()
                                             lblKDJ.Text = queueKDJ.Count
                                         End Sub))
                    log.Debug("KDJ队列剩余" & queueKDJ.Count & "")
                    workItem.DelegateMakeIndexRt.Invoke(workItem.Args(0))
                End If
            End If
            Thread.Sleep(1000)
        Loop Until bStopDoRealThread = True
    End Sub

    Sub ThMakeRSIReal()
        Do
            If queueRSI.Count > 0 Then
                Dim workItem As WorkItemIndex
                Dim result = queueRSI.TryDequeue(workItem)
                If result = True Then
                    Me.Invoke(New Action(Sub()
                                             lblRSI.Text = queueRSI.Count
                                         End Sub))
                    log.Debug("RSI队列剩余" & queueRSI.Count & "")
                    workItem.DelegateMakeIndexRt.Invoke(workItem.Args(0))
                End If
            End If
            Thread.Sleep(1000)
        Loop Until bStopDoRealThread = True
    End Sub

    Sub ThMakeCCIReal()
        Do
            If queueCCI.Count > 0 Then
                Dim workItem As WorkItemIndex
                Dim result = queueCCI.TryDequeue(workItem)
                If result = True Then
                    Me.Invoke(New Action(Sub()
                                             lblCCI.Text = queueCCI.Count
                                         End Sub))
                    log.Debug("CCI队列剩余" & queueCCI.Count & "")
                    workItem.DelegateMakeIndexRt.Invoke(workItem.Args(0))
                End If
            End If
            Thread.Sleep(1000)
        Loop Until bStopDoRealThread = True
    End Sub

    Sub ThMakeWRReal()
        Do
            If queueWR.Count > 0 Then
                Dim workItem As WorkItemIndex
                Dim result = queueWR.TryDequeue(workItem)
                If result = True Then
                    Me.Invoke(New Action(Sub()
                                             lblWR.Text = queueWR.Count
                                         End Sub))
                    log.Debug("WR队列剩余" & queueWR.Count & "")
                    workItem.DelegateMakeIndexRt.Invoke(workItem.Args(0))
                End If
            End If
            Thread.Sleep(1000)
        Loop Until bStopDoRealThread = True
    End Sub

    Sub ThMakeBOLLReal()
        Do
            If queueBOLL.Count > 0 Then
                Dim workItem As WorkItemIndex
                Dim result = queueBOLL.TryDequeue(workItem)
                If result = True Then
                    Me.Invoke(New Action(Sub()
                                             lblBOLL.Text = queueBOLL.Count
                                         End Sub))
                    log.Debug("BOLL队列剩余" & queueBOLL.Count & "")
                    workItem.DelegateMakeIndexRt.Invoke(workItem.Args(0))
                End If
            End If
            Thread.Sleep(1000)
        Loop Until bStopDoRealThread = True
    End Sub

    Sub ThMakeBIASReal()
        Do
            If queueBIAS.Count > 0 Then
                Dim workItem As WorkItemIndex
                Dim result = queueBIAS.TryDequeue(workItem)
                If result = True Then
                    Me.Invoke(New Action(Sub()
                                             lblBIAS.Text = queueBIAS.Count
                                         End Sub))
                    log.Debug("BIAS队列剩余" & queueBIAS.Count & "")
                    workItem.DelegateMakeIndexRt.Invoke(workItem.Args(0))
                End If
            End If
            Thread.Sleep(1000)
        Loop Until bStopDoRealThread = True
    End Sub
     '处理实时MACD指标
        Dim workItemMACD = New WorkItemIndex(New delegateMakeIndex_RT(AddressOf makeMACD_RT),
                               {gi})
        queueMACD.Enqueue(workItemMACD)

        '处理实时KDJ指标
        Dim workItemKDJ = New WorkItemIndex(New delegateMakeIndex_RT(AddressOf makeKDJ_RT),
                               {gi})
        queueKDJ.Enqueue(workItemKDJ)

        '处理实时RSI指标
        Dim workItemRSI = New WorkItemIndex(New delegateMakeIndex_RT(AddressOf makeRSI_RT),
                               {gi})
        queueRSI.Enqueue(workItemRSI)

        '处理实时CCI指标
        Dim workItemCCI = New WorkItemIndex(New delegateMakeIndex_RT(AddressOf makeCCI_RT),
                               {gi})
        queueCCI.Enqueue(workItemCCI)

        '处理实时WR指标
        Dim workItemWR = New WorkItemIndex(New delegateMakeIndex_RT(AddressOf makeWR_RT),
                               {gi})
        queueWR.Enqueue(workItemWR)

        '处理实时BOLL指标
        Dim workItemBOLL = New WorkItemIndex(New delegateMakeIndex_RT(AddressOf makeBOLL_RT),
                               {gi})
        queueBOLL.Enqueue(workItemBOLL)

        '处理实时BIAS指标
        Dim workItemBIAS = New WorkItemIndex(New delegateMakeIndex_RT(AddressOf makeBIAS_RT),
                               {gi})
        queueBIAS.Enqueue(workItemBIAS)

 

 
 
灯塔下的守望者的主页 灯塔下的守望者 | 菜鸟二级 | 园豆:319
提问于:2015-12-08 10:14
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册