想象你有一个IO重函数是这样的:Easy Python ASync。预编译器?
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部分)
我想我可以编写一个'编译器'在执行前更改代码,但它不像最佳解决方案那样缝隙。
你有什么想法,关于如何做上述事情?
使用发电机的好主意! 我制作了一个版本,可以开启和关闭主线程任何次数:code.activestate.com/recipes/576952由于某种原因,如果我使用glib,它会在随机地方出现死锁。我可能不得不问他们有关:) – 2009-11-10 19:07:54