xref: /netbsd-src/external/bsd/unbound/dist/libunbound/python/doc/examples/example2.rst (revision 0cd9f4ecf44538bbdd5619b5b2081449960ab3e6)
1.. _example_setup_ctx:
2
3Lookup from threads
4===================
5
6This example shows how to use unbound module from a threaded program.
7In this example, three lookup threads are created which work in background.
8Each thread resolves different DNS record.
9
10Source code
11-----------
12
13::
14
15    #!/usr/bin/python
16    from unbound import ub_ctx, RR_TYPE_A, RR_CLASS_IN
17    from threading import Thread
18
19    ctx = ub_ctx()
20    ctx.resolvconf("/etc/resolv.conf")
21
22    class LookupThread(Thread):
23        def __init__(self,ctx, name):
24            Thread.__init__(self)
25            self.ctx = ctx
26            self.name = name
27
28        def run(self):
29            print "Thread lookup started:",self.name
30            status, result = self.ctx.resolve(self.name, RR_TYPE_A, RR_CLASS_IN)
31            if status == 0 and result.havedata:
32                print "  Result:",self.name,":", result.data.address_list
33
34    threads = []
35    for name in ["www.fit.vutbr.cz","www.vutbr.cz","www.google.com"]:
36        thread = LookupThread(ctx, name)
37        thread.start()
38        threads.append(thread)
39
40    for thread in threads:
41        thread.join()
42