如何以短时间间隔从 GAE 服务轮询 Web 服务?
How do I poll a web service from a GAE service in short intervals?
我正在开发依赖于 GAE 服务的客户端应用程序。此服务需要通过以小于 1 分钟的间隔轮询远程 Web 服务来获取更新,因此 cron 作业可能不是这里的方式。
我需要从 GAE 服务每隔几秒轮询一次 Web 服务,然后更新客户端应用程序。所以要分解它:
- GAE 服务每 5 秒轮询一次远程 Web 服务。
- 如果进行了更改,请立即更新客户端应用程序。
第 2 步已经解决,但我正在努力寻找一种好的方法来进行这种轮询。我无法控制远程 Web 服务,因此无法对此进行任何更改。
我查看了 Task queue API
,但文档明确指出它是 unsuitable for interactive applications where a user is waiting for the result
解决这个问题的最佳方法是什么?
使用 cron 来调度一堆 taskqueue 任务,错开 etas
def cron_job(): # scheduled to run every 5 minutes
for i in xrange(0, 60*5, 5):
deferred.defer(poll_web_service, _countdown=i)
def poll_web_service():
# do stuff
或者,以这种频率,您还不如在这方面有一个专门的实例。您可以使用手动缩放微服务来做到这一点,并且您可以让 /_ah/start/
的请求处理程序永远不会 return,这将让它永远 运行(除了定期重启)。看到这个:https://cloud.google.com/appengine/docs/standard/python/how-instances-are-managed#instance_scaling
def on_change_detected(params):
queue = taskqueue.Queue('default')
task = taskqueue.Task(
url='/some-url-on-your-default-service/',
countdown=0,
target='default',
params={'params': params})
queue.add(task)
class Start(webapp2.RequestHandler):
def get(self):
while True:
time.sleep(5)
if change_detected: # YOUR LOGIC TO DETECT A CHANGE GOES HERE
on_change_detected()
_routes = [
RedirectRoute('/_ah/start', Start, name='start'),
]
for r in _routes:
app.router.add(r)
我正在开发依赖于 GAE 服务的客户端应用程序。此服务需要通过以小于 1 分钟的间隔轮询远程 Web 服务来获取更新,因此 cron 作业可能不是这里的方式。
我需要从 GAE 服务每隔几秒轮询一次 Web 服务,然后更新客户端应用程序。所以要分解它:
- GAE 服务每 5 秒轮询一次远程 Web 服务。
- 如果进行了更改,请立即更新客户端应用程序。
第 2 步已经解决,但我正在努力寻找一种好的方法来进行这种轮询。我无法控制远程 Web 服务,因此无法对此进行任何更改。
我查看了 Task queue API
,但文档明确指出它是 unsuitable for interactive applications where a user is waiting for the result
解决这个问题的最佳方法是什么?
使用 cron 来调度一堆 taskqueue 任务,错开 etas
def cron_job(): # scheduled to run every 5 minutes
for i in xrange(0, 60*5, 5):
deferred.defer(poll_web_service, _countdown=i)
def poll_web_service():
# do stuff
或者,以这种频率,您还不如在这方面有一个专门的实例。您可以使用手动缩放微服务来做到这一点,并且您可以让 /_ah/start/
的请求处理程序永远不会 return,这将让它永远 运行(除了定期重启)。看到这个:https://cloud.google.com/appengine/docs/standard/python/how-instances-are-managed#instance_scaling
def on_change_detected(params):
queue = taskqueue.Queue('default')
task = taskqueue.Task(
url='/some-url-on-your-default-service/',
countdown=0,
target='default',
params={'params': params})
queue.add(task)
class Start(webapp2.RequestHandler):
def get(self):
while True:
time.sleep(5)
if change_detected: # YOUR LOGIC TO DETECT A CHANGE GOES HERE
on_change_detected()
_routes = [
RedirectRoute('/_ah/start', Start, name='start'),
]
for r in _routes:
app.router.add(r)