time.sleep 卡住了
这是一个奇怪的问题,我只能在我们更强大的生产机器上重现这个问题。
def test_foo(self):
res = self._run_job( ....)
self.assertTrue("Hello Input!" in res.json()["stdout"], res.text)
.........
def _run_job(self, cbid, auth, d):
.........
while True:
res = requests.get(URL+"/status/"+status_id, auth=auth) <--- hangs here
if res.json()["status"] != "Running":
break
else:
time.sleep(2)
..........
我必须中断这个过程,这里是错误追踪信息:
Traceback (most recent call last):
File "test_full.py", line 231, in <module>
unittest.main()
File "/opt/graphyte/vens/gcs/local/lib/python2.7/site-packages/unittest2/main.py", line 98, in __init__
self.runTests()
File "/opt/graphyte/vens/gcs/local/lib/python2.7/site-packages/unittest2/main.py", line 232, in runTests
self.result = testRunner.run(self.test)
File "/opt/graphyte/vens/gcs/local/lib/python2.7/site-packages/unittest2/runner.py", line 162, in run
test(result)
File "/opt/graphyte/vens/gcs/local/lib/python2.7/site-packages/unittest2/suite.py", line 64, in __call__
return self.run(*args, **kwds)
File "/opt/graphyte/vens/gcs/local/lib/python2.7/site-packages/unittest2/suite.py", line 84, in run
self._wrapped_run(result)
File "/opt/graphyte/vens/gcs/local/lib/python2.7/site-packages/unittest2/suite.py", line 114, in _wrapped_run
test._wrapped_run(result, debug)
File "/opt/graphyte/vens/gcs/local/lib/python2.7/site-packages/unittest2/suite.py", line 116, in _wrapped_run
test(result)
File "/opt/graphyte/vens/gcs/local/lib/python2.7/site-packages/unittest2/case.py", line 398, in __call__
return self.run(*args, **kwds)
File "/opt/graphyte/vens/gcs/local/lib/python2.7/site-packages/unittest2/case.py", line 340, in run
testMethod()
File "test_full.py", line 59, in test_session
"cmd": "python helloworld.py"
File "test_full.py", line 129, in _run_job
time.sleep(2)
File "/opt/graphyte/vens/gcs/local/lib/python2.7/site-packages/gevent/hub.py", line 79, in sleep
switch_result = get_hub().switch()
File "/opt/graphyte/vens/gcs/local/lib/python2.7/site-packages/gevent/hub.py", line 164, in switch
return greenlet.switch(self)
KeyboardInterrupt
Exception KeyError: KeyError(155453036,) in <module 'threading' from '/usr/lib/python2.7/threading.pyc'> ignored
为什么会涉及到 gevent
呢?这是一个功能测试。它只是通过 requests
库发起 HTTP 请求,所以可能 switch
是指 requests
。
但是作为一个简单的循环,怎么会出错呢?
2 个回答
1
为什么会涉及到
gevent
呢?
gevent
这个库会对一些标准模块进行“猴子补丁”,也就是对它们进行修改,让它们能够更好地合作工作。其中一个变化就是把 time.sleep
替换成 gevent.sleep
。
http://www.gevent.org/gevent.monkey.html#gevent.monkey.patch_time
1
你在使用gevent的时候,有没有在做猴子补丁?
这可能是因为网络请求在进行中,但出于某种原因一直没有返回结果。我建议你暂时停止猴子补丁,直接在需要的地方使用gevent。
可能是因为现在请求是异步的,它会立即返回,然后再进行“睡眠”(也是异步的),接着再发请求,然后再重复这个过程……