我是蟒蛇和龙卷风的新手。我正在尝试使用协同程序。
def doStuff(callback):
def task():
callback("One Second Later")
Timer(1,task).start()
@gen.coroutine
def routine1():
ans = yield gen.Task(doStuff)
raise gen.Return(ans)
if __name__ == "__main__":
print routine1()
我正在尝试获取doStuff()函数的结果,我希望它是“One Second Later”。但它不起作用。任何帮助,将不胜感激。谢谢
答案 0 :(得分:0)
What's probably happening is, you haven't started the IOLoop, nor are you waiting for your coroutine to complete before your script exits. You'll probably notice your script runs in a couple milliseconds, rather than pausing for a second as it ought. Do this:
if let audioURL = NSURL(string:"http://freetone.org/ring/stan/iPhone_5-Alarm.mp3") {
let documentsDirectoryURL = NSFileManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first as! NSURL
let destinationURL = documentsDirectoryURL.URLByAppendingPathComponent(audioURL.lastPathComponent!)
if let audioData = NSData(contentsOfURL: audioURL) {
audioData.writeToURL(destinationURL, atomically: true)
destinationURL.path!.fileSizeKB // 182KB
destinationURL.movieDuration // 22.704s
}
}