【发布时间】:2010-12-14 08:19:43
【问题描述】:
想象一下你有一个像这样的 io 重函数:
def getMd5Sum(path):
with open(path) as f:
return md5(f.read()).hexdigest()
你认为 Python 是否足够灵活,可以允许这样的代码(注意 $):
def someGuiCallback(filebutton):
...
path = filebutton.getPath()
md5sum = $getMd5Sum()
showNotification("Md5Sum of file: %s" % md5sum)
...
要像这样执行:
def someGuiCallback_1(filebutton):
...
path = filebutton.getPath()
Thread(target=someGuiCallback_2, args=(path,)).start()
def someGuiCallback_2(path):
md5sum = getMd5Sum(path)
glib.idle_add(someGuiCallback_3, md5sum)
def someGuiCallback_3(md5sum):
showNotification("Md5Sum of file: %s" % md5sum)
...
(glib.idle_add 只是将一个函数推送到主线程的队列中)
我曾考虑过使用装饰器,但它们不允许我在调用后访问函数的“内容”。 (showNotification 部分)
我想我可以编写一个“编译器”来在执行之前更改代码,但它不像最佳解决方案。
你有什么想法,关于如何做上面的事情吗?
【问题讨论】:
标签: python compiler-construction syntax asynchronous