Running only one background thread in google appen

2019-06-12 15:55发布

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.

1条回答
家丑人穷心不美
2楼-- · 2019-06-12 16:28

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.

查看更多
登录 后发表回答