Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

是否可以添加SubmitQueue类似的接口 #55

Closed
yangjuncode opened this issue Oct 20, 2019 · 6 comments
Closed

是否可以添加SubmitQueue类似的接口 #55

yangjuncode opened this issue Oct 20, 2019 · 6 comments
Assignees
Labels
invalid This doesn't seem right wontfix This will not be worked on

Comments

@yangjuncode
Copy link

现在submit的任务执行顺序可以认为是随机的,但是有应用的情况是处理数据时有顺序关系,用户可以用队列来处理此类任务,但是每个task handler中都增加了队列的代码,感觉还是有点累赘。

如果ants有SumbitQueue(queueName interface{}, task func())error 这样的接口,则使用起来感觉方便很多

@panjf2000
Copy link
Owner

panjf2000 commented Oct 20, 2019

但是有应用的情况是处理数据时有顺序关系

是我理解有误吗?你这不是串行任务吗?那为什么要用并发来处理,如果是串行的用并发没意义啊,直接单线程处理。

@panjf2000 panjf2000 added the invalid This doesn't seem right label Oct 20, 2019
@yangjuncode
Copy link
Author

一个例子:
一个tcp server,每个socket都可以接收不同的数据包(业务/task)A1,A2,A3,A4,A5, B1,B2,B3,C1,C2,C3,
但是接收并不一定是那么规则,可以是A1,A2,A3,B1,C1,B2,B3,A4,A5,C2,C3,
对于同一个系列当然是串行,A2必须在A1处理完之后处理,C2必须在C1后处理,但是Ax,By,Cz之间是不冲突的,
如果有SumbitQueue,则可以很简化处理.

@panjf2000
Copy link
Owner

panjf2000 commented Oct 20, 2019

用户场景我了解了,但是你期待的解决方案我不是清楚,能具体描述一下吗?或者贴点伪代码看看?

另外,下次提 issue 不要删掉模板自己写,麻烦按照我现在设置的模板填写,这样把需求一次性写清楚可以节省很多反复沟通的时间:

**Is your feature request related to a problem? Please describe.**
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

**Describe the solution you'd like**
A clear and concise description of what you want to happen.

**Describe alternatives you've considered**
A clear and concise description of any alternative solutions or features you've considered.

**Additional context**
Add any other context or screenshots about the feature request here.

@panjf2000 panjf2000 reopened this Oct 20, 2019
@panjf2000
Copy link
Owner

刚才点错了不小心关掉了,忽略。。。

@yangjuncode
Copy link
Author

目前没用ants,需要自己管理队列,需要起很多goroutine

for{
    packet=recv(con)
    switch packet.Type{
    case A:
           go processA(con,packet)
   case B:
          go processB(con,packet)
   ......
}

func processA(con,packet){
    queuename:=calc_queuename(con,A)
   put_packet_in_queue(queuename,packet)

    lock_queue(queuename)
    front_packet:=pop_packet_from_queue(queuename)
   actual_processingA(front_packet)
   unlock_queue(queuename)
}

func processB(con,packet){
    queuename:=calc_queuename(con,B)
   put_packet_in_queue(queuename,packet)

    lock_queue(queuename)
    front_packet:=pop_packet_from_queue(queuename)
   actual_processingB(front_packet)
   unlock_queue(queuename)
}

如果有SubmitQueue,可以简化如下:

for{
    packet=recv(con)
    switch packet.Type{
    case A:
           ants.SubmitQueue(calc_queuename(con,A),func(){
          actual_processingA(packet)
          }
   case B:
           ants.SubmitQueue(calc_queuename(con,B),func(){
          actual_processingB(packet)
          }
   ......
}

@panjf2000 panjf2000 added the wontfix This will not be worked on label Oct 20, 2019
@panjf2000
Copy link
Owner

我看完你这代码,你是想让 ants 来帮你在内部维护一个保证业务顺序的队列?这个提议不太合理,ants 作为一个协程池,它提供的功能很简单,核心价值只有两个:复用 goroutine 和定时清理过期 goroutine,从而在大规模 goroutine 场景下节省系统资源和提升并发性能,你这个需求属于业务层面的了,这个功能不在 ants 的职责范围内,我对 ants 的定位是一个简单的 goroutine pool 库,pool 是没有顺序的,只做上面说的两件最核心的事,不能把太多复杂的业务层面的功能加到 ants 里面。

另外,你这种需求完全可以自己封装一个带锁的队列,其实也不麻烦的,你看你贴的代码里,其实相比较起简化的版本也就多了入队和出队的操作(加锁),这个跟你用不用 ants 没有关系,ants 只是提供复用 goroutine 的功能。所以抱歉,这个功能暂不考虑加到 ants 里。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid This doesn't seem right wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants