Golang,App Engine,Channels和线程安全性

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

Golang, App Engine, Channels and Thread Safety

问题

http://blog.golang.org/go-and-google-app-engine

“此外,虽然 goroutine 和 channel 存在,但在 Go 应用在 App Engine 上运行时,每个实例只运行一个线程。也就是说,所有的 goroutine 都在一个操作系统线程中运行,因此对于给定的客户端请求,没有可用的 CPU 并行性。我们预计这个限制在某个时候会被解除。”

这是在2011年5月的情况。现在还是这样吗?

我有一个应用程序,除了利用 Golang 的速度之外什么都不做;它接收一些输入数据,在内存中对其进行计算,然后返回结果。它从不接触数据存储或任何其他 App Engine API。

我需要让应用程序执行许多计算,最好能够进行一定程度的并行计算。因为我是 Golang 的新手,我只是编写了算法,没有考虑线程安全性。当我一次只发送一个请求时,这个方法运行得很好,但是当我尝试并行发送多个计算时,所有的结果都是错误的。我怀疑,但不能百分之百确定,线程安全性是问题所在,尤其是算法使用了映射,而映射是不线程安全的。

http://golang.org/doc/faq#atomic_maps

所以,如何使我的算法线程安全并实现一定程度的并行计算。

最初的想法是使用 channel,它们似乎是线程安全的:

https://stackoverflow.com/questions/10424144/is-it-possible-to-use-gos-buffered-channel-as-a-thread-safe-queue

但是后来我看到了上面的链接,它暗示 channel 可能不可用。

所以...如果它们不可用,也许我需要设置一个任务队列来进行计算,每次只能执行一个计算。

有人可以告诉我在 Golang App Engine 上实现一定程度的线程安全并行计算的最佳模式吗?

谢谢。

英文:

http://blog.golang.org/go-and-google-app-engine

"Also, although goroutines and channels are present, when a Go app runs on App Engine only one thread is run in a given instance. That is, all goroutines run in a single operating system thread, so there is no CPU parallelism available for a given client request. We expect this restriction will be lifted at some point"

That was in May 2011. Is it still true ?

I have an app which does nothing but take advantage of Golang's speed; takes some input data, performs an in- memory calculation on it, returns the results. Never touches the datastore or any other App Engine APIs.

I need to the app to perform many calculations, ideally with some degree of parallelisation. Because I am a Golang noob I just coded up the algo without any thought to thread safety. This worked fine when I sent one request at a time, but when I tried sending multiple calculations in parallel, all the results were wrong. I suspect, but don't know 100%, that thread safety is the problem, esp as the algo uses maps and maps are not thread safe

http://golang.org/doc/faq#atomic_maps

So. How to make my algo thread safe and get some degree of parallelism.

First thoughts was to use channels, which seem to be thread safe:

https://stackoverflow.com/questions/10424144/is-it-possible-to-use-gos-buffered-channel-as-a-thread-safe-queue

But then I came across the link at the top which suggests channels might not be available.

So .. if they are not available, maybe I need to set up a task queue for the calculation which can only perform one calculation at a time.

Can someone enlighten me on the best pattern for achieving a degree of thread- safe parallelisation on Golang App Engine ?

Thanks.

答案1

得分: 1

Google Appengine目前只允许一个操作系统线程。

要了解操作系统线程、Goroutines及其堆栈以及调度器的工作原理,我建议阅读《可扩展的Go调度器提案》或《Go运行时调度器分析》。

由于您可以拥有多个通道和单个线程上的Goroutines,所以在并发方面,Appengine可以很好地工作。如果您需要并行性(即不仅运行多个Goroutines,而且在多个处理器上运行它们),那么目前Appengine可能不是最佳选择,因为它的GOMAXPROCS设置为1。

至于您的具体代码问题,您没有提供任何代码供我们查看和帮助调试您的竞态条件。如果您自己想要调试,您可以阅读这篇关于使用Go竞态检测器的博文。

英文:

Google Appengine only allows one OS thread at the moment.

To understand how OS Threads; Goroutines, and their stacks; and the scheduler work, I recommend reading the Scalable Go Scheduler proposal or the Analysis of the Go Runtime Scheduler.

Since you can have multiple channels, goroutines on a single thread, Appengine will do fine with concurrency. If you need parallelism (i.e. running not only multiple goroutines, but running them over many processors), then Appengine is not the answer at the moment as its GOMAXPROCS is set to one.

As far as your specific code is concerned, you've provided none that we can look at and help debug your race condition. To do so yourself, you'd benefit from reading this blog post about, and using, the Go Race Detector

答案2

得分: 0

我不知道App Engine当前是否支持多个操作系统线程,但即使只有一个操作系统级线程可用,你仍然可以完全使用通道。你提供的链接确实提到了“goroutines和channels存在”,只是它们都在一个操作系统级线程中处理。

英文:

I don't know if app engine currently supports multiple operating system threads, but you can absolutely use channels even when only one OS-level thread is available. The link you provide does state "goroutines and channels are present", they're just all handled in one OS-level thread.

huangapple
  • 本文由 发表于 2014年9月5日 20:13:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/25685654.html
匿名

发表评论

匿名网友

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

确定