duanjian4331
2014-09-05 12:13
浏览 35
已采纳

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:

Is it possible to use Go's 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.

图片转代码服务由CSDN问答提供 功能建议

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

”“尽管goroutine和通道是 当前,当Go应用在App Engine上运行时,在给定实例中仅运行一个线程,也就是说,所有goroutine在单个操作系统线程中运行,因此对于给定的客户端请求没有可用的CPU并行性。 将会在某个时候被解除。”

那是2011年5月。仍然是真的吗?

我有一个仅能利用优势的应用程序 Golang的速度; 接收一些输入数据,对其进行内存计算,然后返回结果。 永远不要接触数据存储区或任何其他App Engine API。

我需要让该应用执行许多计算,理想情况下需要进行一定程度的并行化。 因为我是Golang菜鸟,所以我只是在没有考虑线程安全的情况下对算法进行了编码。 当我一次发送一个请求时,这种方法工作正常,但是当我尝试并行发送多个计算时,所有结果都是错误的。 我怀疑但不是100%知道线程安全是问题,尤其是因为算法使用的是地图,而地图也不是线程安全的

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

如此。 如何使我的算法线程安全并获得一定程度的并行性。

首先想到的是使用似乎是线程安全的通道:

是否可以使用Go的 缓冲通道作为线程安全队列?

但是随后我遇到了顶部的链接,这表明通道可能不可用。

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

有人可以启发我 在Golang App Engine上实现一定程度的线程安全并行化的最佳模式?

谢谢。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dsfs1233 2014-09-05 13:30
    已采纳

    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

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • doushou6480 2014-09-05 13:27

    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.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题