example2.rst revision 238106
1238106Sdes.. _example_setup_ctx: 2238106Sdes 3238106Sdes============================== 4238106SdesLookup from threads 5238106Sdes============================== 6238106Sdes 7238106SdesThis example shows how to use unbound module from a threaded program. 8238106SdesIn this example, three lookup threads are created which work in background. 9238106SdesEach thread resolves different DNS record. 10238106Sdes 11238106Sdes:: 12238106Sdes 13238106Sdes #!/usr/bin/python 14238106Sdes from unbound import ub_ctx, RR_TYPE_A, RR_CLASS_IN 15238106Sdes from threading import Thread 16238106Sdes 17238106Sdes ctx = ub_ctx() 18238106Sdes ctx.resolvconf("/etc/resolv.conf") 19238106Sdes 20238106Sdes class LookupThread(Thread): 21238106Sdes def __init__(self,ctx, name): 22238106Sdes Thread.__init__(self) 23238106Sdes self.ctx = ctx 24238106Sdes self.name = name 25238106Sdes 26238106Sdes def run(self): 27238106Sdes print "Thread lookup started:",self.name 28238106Sdes status, result = self.ctx.resolve(self.name, RR_TYPE_A, RR_CLASS_IN) 29238106Sdes if status == 0 and result.havedata: 30238106Sdes print " Result:",self.name,":", result.data.address_list 31238106Sdes 32238106Sdes threads = [] 33238106Sdes for name in ["www.fit.vutbr.cz","www.vutbr.cz","www.google.com"]: 34238106Sdes thread = LookupThread(ctx, name) 35238106Sdes thread.start() 36238106Sdes threads.append(thread) 37238106Sdes 38238106Sdes for thread in threads: 39238106Sdes thread.join() 40238106Sdes 41238106Sdes 42