在Google Appengine后端只运行一个后台线程 [英] Running only one background thread in google appengine backend

查看:172
本文介绍了在Google Appengine后端只运行一个后台线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如文档中所述,我正在运行后台线程一个无限次后台,用于一些连续的后台处理。

  import com.google.appengine.api.ThreadManager; 
import java.util.concurrent.AtomicLong;

AtomicLong counter = new AtomicLong();

Thread thread = ThreadManager.createBackgroundThread(new Runnable(){
public void run(){
try {
while(true){
counter .doStuff()
Thread.sleep(10);
}
} catch(InterruptedException ex){
throw new RuntimeException(Interrupted in loop:,ex);
}
}
});
thread.start();

我已将此代码绑定到_ah / start端点,以便在实例启动后执行。然而,当在本地服务器上运行时,我发现_ah / start请求在运行时会多次出现,并启动多个此类线程。我一次只需要一个线程来减少后台处理中的争用。



有没有什么办法可以抓住现有的bg线程并检查它的运行情况,以便避免创建新的线程?

更新
在将此代码上传到云后,我还观察到每次在后端调用_ah / start时,它都会在_ah / background端点中产生一个新线程并继续运行。这样,如果我们无法停止早期的线程,就会有很多线程一起运行。 解决方案

后端实例可能不是你需要什么。



以下是事情:你想在服务器上运行无限处理。后端并不意味着这一点。您可能需要考虑在计算引擎上为该特定情况分配虚拟机。



后端用于在后台运行长操作。这些操作通常由用户请求。你的情况是不同的,这就是为什么我会考虑虚拟机场景。



无论如何,特别是对于你遇到的问题,运行线程的实例并不实际接收多个_ah /启动请求。每个实例只收到其中一个请求。但是,系统试图用多个_ah / start请求分离多个实例。我不知道为什么,但显然有些东西要求多次出现后端。



你可以做什么来避免内存中的多个线程是添加一个标志在任何一个线程运行的Memcache中(这是所有后端共享的)。每次线程启动时,检查该标志是否不存在。如果是这样,请不要启动线程。



确保您还实现了_ah / stop请求,以从memcache中移除该标志。



希望这会有所帮助。


As mentioned in documentation I am running a background thread in a backend with 1 instance infinitely for some continuous background processing.

import com.google.appengine.api.ThreadManager;
import java.util.concurrent.AtomicLong;

AtomicLong counter = new AtomicLong();

Thread thread = ThreadManager.createBackgroundThread(new Runnable() {
  public void run() {
    try {
      while (true) {
        counter.doStuff()
        Thread.sleep(10);
      }
    } catch (InterruptedException ex) {
      throw new RuntimeException("Interrupted in loop:", ex);
    }
  }
});
thread.start();

I have tied this code to _ah/start endpoint so that it gets executed once the instance is started. However while running in local server I see _ah/start request is coming in multiple times during the runtime and it starts multiple such thread . I need only one thread to exist at a time to reduce contention in my background processing.

Is there any way to catch hold of the existing bg thread and check if its running so that new thread creation can be avoided ?

Update After uploading this code to cloud I also observed that every time _ah/start is invoked on the backend it spawns a new thread as part of _ah/background endpoint and keeps running . In this way there would be many threads running together if we fail to stop the earlier ones.

解决方案

A backend instance might not be what you need.

Here is the thing: you want to run infinite processing on the server. A backend is not meant for that. You might want to consider spinning off a VM on Compute Engine for that specific scenario.

A backend is for running long operations in the background. These operations are usually requested by the user. Your case is different, and that's why I'd consider the VM scenario.

Anyway, specifically to the problem you are having, your instance running the thread is not actually receiving multiple _ah/start requests. Each instance receives only one of these requests. However, the system is trying to spin off multiple instances with several _ah/start requests. I'm not sure why, but apparently something is asking for that backend to come up multiple times.

What you can do to avoid multiple threads in memory is add a flag in Memcache (which is shared for all the backends) whenever one thread runs. Every time the thread starts, check that the flag doesn't exist. If it does, don't start the thread.

Make sure you also implement the _ah/stop request to remove the flag from memcache.

Hope this helps.

这篇关于在Google Appengine后端只运行一个后台线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆