Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 永久while循环对可伸缩程序有害吗?_Python_Scalability_Gevent - Fatal编程技术网

Python 永久while循环对可伸缩程序有害吗?

Python 永久while循环对可伸缩程序有害吗?,python,scalability,gevent,Python,Scalability,Gevent,假设我需要检索作业状态,并生成一个新线程来处理作业计算: def post_view(request): g = Greenlet.spawn(heavy_job, arg1, arg2) return json.dumps(default_response) 然后在繁重的工作中 def heavy_job(...): # call a 3rd party service to start doing its job job_id = (....) re

假设我需要检索作业状态,并生成一个新线程来处理作业计算:

def post_view(request):
     g = Greenlet.spawn(heavy_job, arg1, arg2)
     return json.dumps(default_response)
然后在繁重的工作中

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请求到达时查询第三方服务?

对不起,我还不能写评论,所以我在这里写了一篇关于Joel Cornett的评论的注释,并举例说明了“while”和“for”的时间安排

这个例子中有一个错误:timeit()方法只执行一次初始化代码,但主代码执行了很多次,因此使用“while”循环,除了第一次执行外,每次执行都不执行任何操作,因为“i”的值已经是10。您可以将10更改为100或1000,并且se在计时上没有差异(但不能使用“for”循环)。您还可以在“while”之前添加“i=0”,并看到非常不同的结果


但是,我认为,这对您的代码并不重要,因为第三方服务.query()可能比“for”和“while”循环之间的差异花费的时间要多得多。

在python中,
for
循环比
while
循环快。您是建议使用生成器吗?我可以编写一个简单的生成器,其中包含
3rdpart servier.query(…)
,生成器接受
作业id
。但是它能同时处理两个用户吗?@AshwiniChaudhary:
>>导入timeit>>>tfor=timeit.Timer(“对于l:t+=i”,“t,l=0,范围(10)”)>>twile=timeit.Timer(““while i<10:t+=i+=1”“,“t=i=0”)>>tfor.timeit()0.676494836807251>>twile.timeit()0.0510928630888574>>
轮询通常不适合扩展。您无法从该第三方获得回调?