登录
首页 >  Golang >  Go问答

在Golang中能否实现进程池

来源:stackoverflow

时间:2024-03-09 17:33:27 358浏览 收藏

哈喽!今天心血来潮给大家带来了《在Golang中能否实现进程池》,想必大家应该对Golang都不陌生吧,那么阅读本文就都不会很困难,以下内容主要涉及到,若是你正在学习Golang,千万别错过这篇文章~希望能帮助到你!

问题内容

我们有一个异步任务调度系统,它使用golang的exec.command来执行php脚本。每次,调度程序从消息队列中获取任务时,都会创建一个新的 exec.command 来执行该任务。

有时,几乎有数千个任务需要同时执行。在这种情况下,调度程序将创建数千个 exec.command,然后在一段时间后销毁它们。

我想知道是否有办法创建进程池(或类似的东西),以便我们可以重用 exec.command 来减少创建新子进程的成本。

ps:我注意到 exec.command 在调用 run 后无法重用。

更新 当前的逻辑如下:

func nsqMessageHandler(msg *nsq.Message) error{

      var task Task
      err:= json.Unmarshal(msg.Body,&task)
      ....
      cmd:=exec.Command("php",task.Payload...)
      err:=cmd.Start()
      ....
      err= cmd.Run()
      err=cmd.Process.Kill()
      ...
 }

解决方案


绝对。您可以创建一个向其提交作业的阻止渠道。然后,您可以通过生成将 job<-channel 作为输入的 goroutines 来创建一组“workers”,并具有例如用于发布进度或结果的输出通道。工人们现在只需在频道上阅读,一旦其中一个得到工作,它就会开始工作。

您希望防止工作人员从一开始就关闭,因此您需要某种方法来阻止它们。解决此问题的一种方法是使用 waitgroup 并让工作人员在其中一个关闭时将 waitgroup 索引减一。此外,你想停止工人,但你不能从外部停止 goroutine - 所以你必须实现一项工作,你可以将其传递给工作人员,迫使他们自己停止。

来自 gobyexample.com 的示例

// In this example we'll look at how to implement
// a _worker pool_ using goroutines and channels.

package main

import "fmt"
import "time"

// Here's the worker, of which we'll run several
// concurrent instances. These workers will receive
// work on the `jobs` channel and send the corresponding
// results on `results`. We'll sleep a second per job to
// simulate an expensive task.
func worker(id int, jobs <-chan int, results chan<- int) {
    for j := range jobs {
        fmt.Println("worker", id, "started  job", j)
        time.Sleep(time.Second)
        fmt.Println("worker", id, "finished job", j)
        results <- j * 2
    }
}

func main() {

    // In order to use our pool of workers we need to send
    // them work and collect their results. We make 2
    // channels for this.
    jobs := make(chan int, 100)
    results := make(chan int, 100)

    // This starts up 3 workers, initially blocked
    // because there are no jobs yet.
    for w := 1; w <= 3; w++ {
        go worker(w, jobs, results)
    }

    // Here we send 5 `jobs` and then `close` that
    // channel to indicate that's all the work we have.
    for j := 1; j <= 5; j++ {
        jobs <- j
    }
    close(jobs)

    // Finally we collect all the results of the work.
    for a := 1; a <= 5; a++ {
        <-results
    }
}

好了,本文到此结束,带大家了解了《在Golang中能否实现进程池》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>