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