Best practices for GAE?

xuanbao · · 760 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I will start a project using GAE and I don&#39;t have much experience with it. So I&#39;m here for wisdom.</p> <p>Is there any place (besides the docs) with some good practices, tools, tricks, anything useful when working with GAE?</p> <hr/>**评论:**<br/><br/>natdm: <pre><p>Search YouTube for Todd McLeod and there&#39;s a playlist with 140+ videos that, towards the end, all rely on GAE.</p></pre>darkmigore: <pre><p>Wow, that is a lot of materials. Thanks!</p></pre>greeneggsnspaghetti: <pre><p>Handy! I hope he covers using urlfetch in a go routine I seem to be having trouble with that (works locally not on prod)</p></pre>BeBimBomb: <pre><p>I watched all 140 episodes. Great content. Go routines are not supported in app engine. You can however use tasks and will need to rethink your app flow a bit</p></pre>greeneggsnspaghetti: <pre><p>Is there a statement anywhere saying go routines dont work with GAE tools? It doesn&#39;t make sense to be forced to do everything on main thread </p></pre>christopherhesse: <pre><p>Well, classic GAE only runs on a single thread. But you can use goroutines. What you can&#39;t do is have your goroutines outlive the request that spawned them.</p> <p>So using urlfetch in a goroutine is straightforward as long as you make sure the urlfetch finishes before the request returns. If you want it to be asynchronous with the request you have to use taskqueue.</p></pre>greeneggsnspaghetti: <pre><p>Ah this makes sense... somewhat frustrating unfortunately! </p> <p>Thanks for that.</p></pre>christopherhesse: <pre><p>Sorry, that wasn&#39;t entirely accurate. I thought you were using frontend instances but you mentioned below that you use backend instances, which don&#39;t have this limitation. Just use runtime.RunInBackground: <a href="https://cloud.google.com/appengine/docs/go/modules/runtime" rel="nofollow">https://cloud.google.com/appengine/docs/go/modules/runtime</a></p> <p>ManagedVMs should also not have this issue, there looks to be a BackgroundContext() call for those: <a href="https://github.com/golang/appengine/blob/0b869404e81685c5f3f2e5126bc2309215c7ad72/appengine_vm.go#L54" rel="nofollow">https://github.com/golang/appengine/blob/0b869404e81685c5f3f2e5126bc2309215c7ad72/appengine_vm.go#L54</a></p></pre>greeneggsnspaghetti: <pre><p>Oh man I somehow missed reading that. Ive been working with GAE for more than a year in PHP but recently started rewriting core over to Go. There is too much to read! Thank you soo much! :)</p></pre>docoptix: <pre><p>Goroutines don&#39;t make much sense for GAE because you have to assume instances being killed and spawned all the time. The delay package should fit you case well.</p></pre>greeneggsnspaghetti: <pre><p>I am using a backend instance. All i wanted was for unzipping to happen asynchronously (and immediately) and to return an ok response to the client. I went back to using cron jobs.</p></pre>gbitten: <pre><blockquote> <p>g a backend instance. All i wanted was for unzipping to happen asynchronously (and immediately) and to return an ok response to the client. I went back t</p> </blockquote> <p>I am not sure but I think you can do that using App Engine Managed VM Environment. <a href="https://cloud.google.com/appengine/docs/managed-vms/" rel="nofollow">https://cloud.google.com/appengine/docs/managed-vms/</a></p></pre>BeBimBomb: <pre><p>Go on app engine runs everything in one main thread. I seem to remember one of those videos from Caleb mentioning no support for goroutines on app engine.</p> <p>Found this: &#34;Although goroutines and channels are present, when a Go app runs on App Engine only one thread is run in a given instance,&#34; the Go team members said. &#34;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.&#34; <a href="http://www.cnet.com/news/google-app-engine-goes-for-go-language/" rel="nofollow">http://www.cnet.com/news/google-app-engine-goes-for-go-language/</a></p></pre>CaptaincCodeman: <pre><p>A few things I&#39;ve learnt from using AppEngine that I can think of off the top of my head</p> <p>Prefer to pay more for datastore writes if it improves read performance - assuming you read data a lot more than write it.</p> <p>You can create your own &#39;composite index fields&#39; that aren&#39;t in your struct which saves money vs indexed properties + a composite index.</p> <p>You can host multiple servers in a single instance - switch on hostname (e.g. api.server.com, auth.server.com).</p> <p>Have a separate module for front-end assets (e.g. SPA) costs nothing in instance hours.</p> <p>Version assets (scripts, css etc...) and serve with long cache times - like a CDN.</p> <p>Use task queue / delay for anything expensive that doesn&#39;t need to be in the immediate response.</p> <p>Go is significantly better at processing than python when it comes to mapreduce operations over the datastore (no more ndb memory issues, tens of thousands of entities in a single task request).</p> <p>Don&#39;t be afraid to use the Managed VM option if you want the best of PaaS scaling and flexibility of custom runtime (docker container). GroupCache + the GCE API can make auto-setup of a cluster easy (and cheaper than the memcache service which now seems insanely expensive).</p> <p>It takes a little extra work to setup but it is possible to make app engine an implementation detail. By that, I mean you use an interface to allow different storage backends and limit dependencies on appengine so you can run your app elsewhere using MongoDB or whatever. Even if you only ever run on appengine you still get the benefit of having isolated testable modules without having to fire-up the local dev-server in your tests.</p></pre>Zilog8: <pre><p>GAE = <del>Golang</del> Google App Engine</p> <blockquote> <p>to save others the trouble of looking it up.</p> </blockquote> <p>Edit:</p> <blockquote> <p>Changed Golang to Google, cause it makes a huge difference. <a href="https://www.youtube.com/watch?v=XxFUgMDRyUI&amp;index=93&amp;list=PLSak_q1UXfPrI6D67NF8ajfeJ6f7MH83S" rel="nofollow">Got confused by this guy&#39;s labeling of this video</a>.</p> </blockquote></pre>cawdrizzle: <pre><p>Err, Google App Engine is the product. It comes in Java, Python, PHP and go.</p></pre>Zilog8: <pre><p>Changed it. <a href="https://www.youtube.com/watch?v=XxFUgMDRyUI&amp;index=93&amp;list=PLSak_q1UXfPrI6D67NF8ajfeJ6f7MH83S" rel="nofollow">Got confused by this guy&#39;s labeling of this video</a>.</p></pre>GoTheFuckToBed: <pre><p>People not using acronyms are the real mvp</p></pre>elithrar_: <pre><p>Ask over on the Gophers Slack community (<a href="https://gophersinvite.herokuapp.com/" rel="nofollow">https://gophersinvite.herokuapp.com/</a>) as well. Lots of GCE users there. </p></pre>darkmigore: <pre><p>Will do, thanks!</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

760 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传