Golang 后台处理

huangapple go评论90阅读模式
英文:

Golang background processing

问题

在Go语言中,可以通过使用goroutine和channel来实现后台处理和队列功能。

首先,你可以使用goroutine来在后台执行任务。Goroutine是Go语言中的轻量级线程,可以并发地执行函数或方法。你可以使用关键字"go"来启动一个goroutine,将任务放在一个匿名函数中并在其中执行。例如:

go func() {
    // 执行后台任务的代码
}()

其次,你可以使用channel来进行任务的队列管理。Channel是Go语言中用于在goroutine之间进行通信的机制。你可以创建一个带有缓冲区的channel,将任务放入channel中,然后在后台的goroutine中从channel中读取任务并执行。例如:

tasks := make(chan Task, 10) // 创建一个带有缓冲区大小为10的channel

// 后台的goroutine,从channel中读取任务并执行
go func() {
    for task := range tasks {
        // 执行任务的代码
    }
}()

// 将任务放入channel中
tasks <- task

通过结合使用goroutine和channel,你可以实现后台处理和队列功能。你可以将需要在后台执行的任务放入channel中,后台的goroutine会从channel中读取任务并执行。这样可以确保任务在后台执行,而不会阻塞主线程。

希望这个简单而可靠的解决方案对你有帮助!

英文:

How can one do background processing/queueing in Go?

For instance, a user signs up, and you send them a confirmation email - you want to send the confirmation email in the background as it may be slow, and the mail server may be down etc etc.

In Ruby a very nice solution is DelayedJob, which queues your job to a relational database (i.e. simple and reliable), and then uses background workers to run the tasks, and retries if the job fails.

I am looking for a simple and reliable solution, not something low level if possible.

答案1

得分: 9

虽然你可以打开一个goroutine并执行所有异步任务,但如果你想要可靠性,即触发任务后能够确保任务完成,这并不是一个很好的解决方案。

如果你真的需要生产级别的解决方案,可以选择使用分布式工作队列。我不知道有哪些特定适用于golang的队列,但你可以使用rabbitmq、beanstalk、redis或类似的队列引擎来卸载这些任务,同时增加容错性和队列持久化。

英文:

While you could just open a goroutine and do every async task you want, this is not a great solution if you want reliability, i.e. the promise that if you trigger a task it will get done.

If you really need this to be production grade, opt for a distributed work queue. I don't know of any such queues that are specific to golang, but you can work with rabbitmq, beanstalk, redis or similar queuing engines to offload such tasks from your process and add fault tolerance and queue persistence.

答案2

得分: 8

一个简单的Goroutine可以完成这个任务:
http://golang.org/doc/effective_go.html#goroutines

打开一个Goroutine来处理电子邮件发送,然后回复HTTP请求或其他操作。

如果你想使用工作队列,可以使用Rabbitmq或Beanstalk客户端,比如:
https://github.com/streadway/amqp
https://github.com/kr/beanstalk

或者你可以在你的进程中创建一个带有FIFO队列的Goroutine:
https://github.com/iNamik/go_container

但也许最好的解决方案是使用这个作业队列库,你可以设置并发限制等:
https://github.com/otium/queue

import "github.com/otium/queue"

q := queue.NewQueue(func(email string) {
     //你的邮件发送代码
}, 20)

q.Push("foo@bar.com")
英文:

A simple Goroutine can make the job:
http://golang.org/doc/effective_go.html#goroutines

Open a gorutine with the email delivery and then answer to the HTTP request or whatever

If you wish use a workqueue you can use Rabbitmq or Beanstalk client like:
https://github.com/streadway/amqp
https://github.com/kr/beanstalk

Or maybe you can create a queue in you process with a FIFO queue running in a goroutine
https://github.com/iNamik/go_container

But maybe the best solution is this job queue library, with this library you can set the concurrency limit, etc:
https://github.com/otium/queue

import &quot;github.com/otium/queue&quot;

q := queue.NewQueue(func(email string) {
     //Your mail delivery code
}, 20)

q.Push(&quot;foo@bar.com&quot;)

答案3

得分: 6

我已经创建了一个使用消息队列运行异步任务的库(目前支持RabbitMQ和Memcache作为代理,但其他代理如Redis或Cassandra也可以很容易地添加进来)。

你可以看一下。它可能足够适合你的使用情况(它还支持链式调用和工作流)。

https://github.com/RichardKnop/machinery

不过,这是一个早期阶段的项目。

英文:

I have created a library for running asynchronous tasks using a message queue (currently RabbitMQ and Memcache are supported brokers but other brokers like Redis or Cassandra could easily be added).

You can take a look. It might be good enough for your use case (and it also supports chaining and workflows).

https://github.com/RichardKnop/machinery

It is an early stage project though.

答案4

得分: 3

你也可以使用goworker库来调度任务。

http://www.goworker.org/

英文:

You can also use goworker library to schedule jobs.

http://www.goworker.org/

答案5

得分: 0

如果你来自Ruby背景,并且正在寻找类似于Sidekiq、Resque或DelayedJob的东西,请查看库asynq

队列语义与sidekiq非常相似。

https://github.com/hibiken/asynq

英文:

If you are coming from Ruby background and looking for something like Sidekiq, Resque, or DelayedJob, please check out the library asynq.

Queue semantics are very similar to sidekiq.

https://github.com/hibiken/asynq

答案6

得分: 0

如果你想要一个非常简单易用、稳定可靠且类似Go风格的库,它使用Redis作为后端和RabbitMQ作为消息代理,你可以尝试使用https://github.com/Joker666/cogman。

英文:

If you want a library with a very simple interface, yet robust that feels Go-like, uses Redis as Backend and RabbitMQ as message broker, you can try

https://github.com/Joker666/cogman

huangapple
  • 本文由 发表于 2014年2月13日 16:25:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/21748716.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定