13b6c3722Schristos.. _example_asynch: 23b6c3722Schristos 33b6c3722SchristosAsynchronous lookup 4*0cd9f4ecSchristos=================== 53b6c3722Schristos 63b6c3722SchristosThis example performs the name lookup in the background. 73b6c3722SchristosThe main program keeps running while the name is resolved. 83b6c3722Schristos 9*0cd9f4ecSchristosSource code 10*0cd9f4ecSchristos----------- 11*0cd9f4ecSchristos 123b6c3722Schristos:: 133b6c3722Schristos 143b6c3722Schristos #!/usr/bin/python 153b6c3722Schristos import time 163b6c3722Schristos import unbound 173b6c3722Schristos 183b6c3722Schristos ctx = unbound.ub_ctx() 193b6c3722Schristos ctx.resolvconf("/etc/resolv.conf") 203b6c3722Schristos 213b6c3722Schristos def call_back(my_data,status,result): 223b6c3722Schristos print "Call_back:", my_data 233b6c3722Schristos if status == 0 and result.havedata: 243b6c3722Schristos print "Result:", result.data.address_list 253b6c3722Schristos my_data['done_flag'] = True 263b6c3722Schristos 273b6c3722Schristos 283b6c3722Schristos my_data = {'done_flag':False,'arbitrary':"object"} 293b6c3722Schristos status, async_id = ctx.resolve_async("www.seznam.cz", my_data, call_back, unbound.RR_TYPE_A, unbound.RR_CLASS_IN) 303b6c3722Schristos 313b6c3722Schristos while (status == 0) and (not my_data['done_flag']): 323b6c3722Schristos status = ctx.process() 333b6c3722Schristos time.sleep(0.1) 343b6c3722Schristos 353b6c3722Schristos if (status != 0): 363b6c3722Schristos print "Resolve error:", unbound.ub_strerror(status) 373b6c3722Schristos 38*0cd9f4ecSchristosThe :meth:`unbound.ub_ctx.resolve_async` method is able to pass on any Python 39*0cd9f4ecSchristosobject. In this example, we used a dictionary object ``my_data``. 40