#! /usr/bin/env python import sys from twisted.internet import reactor from example_secrets import mydb, main def print_user_info(user): print "looking up", user d = mydb.get_user(user) print "still thinking.." def got_info(res): print "name:", res.name reactor.stop() d.addCallback(got_info) print "*still* thinking.." main(print_user_info, sys.argv[1])
#! /usr/bin/env python import sys from twisted.internet import reactor from example_secrets import get_page_title, main def print_page_title(url): print "fetching", url d = get_page_title(url) def got_info(title): print "title:", title reactor.stop() def fail_info(f): print "failed!", f.type, f.value reactor.stop() d.addCallbacks(got_info, fail_info) main(print_page_title, sys.argv[1])
#! /usr/bin/env python import sys from twisted.internet import reactor from example_secrets import mydb, main def get_user_name(user): d = mydb.get_user(user) def extract_name(res): print "extract_name(%r)" % res if res: return res.name return "(unknown)" d.addCallback(extract_name) return d def print_user_name(user): d = get_user_name(user) def print_name(name): print "print_name(%r)" % name print "Name:", name reactor.stop() d.addCallback(print_name) print "print_user_name returning" main(print_user_name, sys.argv[1])