Python:为执行计划块/闭包
我有一些代码,我想异步计划执行。 我试过这样做:Python:为执行计划块/闭包,python,python-3.x,Python,Python 3.x,我有一些代码,我想异步计划执行。 我试过这样做: time.sleep(3) print("Do stuff") 但这阻碍了我的剧本,而且看起来也有点愚蠢 如何用Python重写此Javascript代码 setTimeout(function() { print("Do stuff") }.bind(this), 3000); 我想到了这个: import threading thread = threading.Timer(3.0, do_stuff) thread.start(
time.sleep(3)
print("Do stuff")
但这阻碍了我的剧本,而且看起来也有点愚蠢
如何用Python重写此Javascript代码
setTimeout(function() {
print("Do stuff")
}.bind(this), 3000);
我想到了这个:
import threading
thread = threading.Timer(3.0, do_stuff)
thread.start()
我不想使用线程。线程是愚蠢的。如果使用Tornado,答案是:
tornado.ioloop.ioloop.current().add\u timeout(timedelta(seconds=1),dou stuff
)
不需要使用线程为什么我会得到一个负分?这是一个非常有效的问题。Javascript在语言核心中内置了一个事件循环。Python没有。如果没有线程或事件循环,像
setTimeout
这样的事情就没有多大意义。好吧,那么Tornado是如何工作的呢?Tornado有一个事件循环。(我不知道它还能做什么——我不使用Tornado——但谷歌Tornado事件循环显示它使用了一个。)