example3.rst revision 303975
1.. _example_asynch: 2 3============================== 4Asynchronous lookup 5============================== 6 7This example performs the name lookup in the background. 8The main program keeps running while the name is resolved. 9 10:: 11 12 #!/usr/bin/python 13 import time 14 import unbound 15 16 ctx = unbound.ub_ctx() 17 ctx.resolvconf("/etc/resolv.conf") 18 19 def call_back(my_data,status,result): 20 print "Call_back:", my_data 21 if status == 0 and result.havedata: 22 print "Result:", result.data.address_list 23 my_data['done_flag'] = True 24 25 26 my_data = {'done_flag':False,'arbitrary':"object"} 27 status, async_id = ctx.resolve_async("www.seznam.cz", my_data, call_back, unbound.RR_TYPE_A, unbound.RR_CLASS_IN) 28 29 while (status == 0) and (not my_data['done_flag']): 30 status = ctx.process() 31 time.sleep(0.1) 32 33 if (status != 0): 34 print "Resolve error:", unbound.ub_strerror(status) 35 36The :meth:`unbound.ub_ctx.resolve_async` method is able to pass on any Python object. In this example, we used a dictionary object `my_data`. 37