假设我需要检索作业状态,并生成一个新线程来处理作业计算:
def post_view(request):
g = Greenlet.spawn(heavy_job, arg1, arg2)
return json.dumps(default_response)
然后在heavy_job
def heavy_job(...):
# call a 3rd party service to start doing its job
job_id = (....)
request.session['heavy_job_status'] = 'Running'
status = 'Running'
while status == 'Running':
# ask the 3rd party guy "are you still running"
resp = 3rdparty-service.query(job_id, ....)
if resp != 'Running':
return resp
time.sleep(5) # we will monkey patch the time with gevent's timer so no locking
这种while循环方法真的不适合缩放吗?还是我宁愿在每个 ajax 请求进来时查询 3rd 方服务?