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.

  • 写回答

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)