我只是想明白,如果当我调用time.sleep(x)时,运行代码的当前线程会延迟x秒。但是,这是否释放了处理器的这些x秒或者线程是否将资源保留给自己,并且在x秒后它才开始执行下一行代码。time.sleep()函数
与我对着确切的情况编辑:
这里的情况跟我说:
class SomeHandler(tornado.websocket.WebSocketHandler)
@tornado.gen.coroutine
def something_async():
time.sleep(5)
return result
def on_message(message):
future = yield something_async(message)
if __name__ == '__main__':
application = tornado.web.Application([
(r'/', SomeHandler),
])
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(8888)
tornado.ioloop.IOLoop.instance().start()
现在,因为这场龙卷风将是一个单线程的服务器,究竟做time.sleep(5)在这种情况下(它会阻塞线程5秒使整个进程同步)还是协程会产生一个新的线程?
对CPU进行处理并阻止任何其他线程执行有意义吗?有没有可能找到一种方法? –
也许这可以帮助你:http://stackoverflow.com/questions/92928/time-sleep-sleeps-thread-or-process – Querenker
简单的答案是,它将释放处理器,但实现可能会有所不同。有一种称为*旋转锁定的功能*,如果等待时间很短,线程将保留CPU。可以想象,一个实现可以使用自旋锁来实现短暂的睡眠时间,特别是在专门为此提供API的Windows上。另见http://stackoverflow.com/questions/7273474/behavior-of-pythons-time-sleep0-under-linux-does-it-cause-a-context-switch和http://stackoverflow.com/questions/ 22115831/how-to-resolve-spinlock-issues-with-multithreaded-python – cdarke