Code-Snippet:
#!/usr/bin/env python import socket from multiprocessing.pool import ThreadPool import pprint jobs = ("www.heise.de","www.google.com","www.golem.de","www.google.de","www.lanbugs.de","www.microsoft.com") def worker(domain): print socket.gethostbyname(domain) return socket.gethostbyname(domain) pool = ThreadPool(processes=3) result_buffer = {} for d in jobs: print "start " + d async_result = pool.apply_async(worker, args=(d,)) result_buffer[d]=async_result.get() pprint.pprint(result_buffer)
Ausgabe:
>python thread_with_result.py start www.heise.de 193.99.144.85 start www.google.com 172.217.20.68 start www.golem.de 109.68.230.138 start www.google.de 172.217.20.99 start www.lanbugs.de 81.169.181.94 start www.microsoft.com 104.108.168.41 {'www.golem.de': '109.68.230.138', 'www.google.com': '216.58.207.68', 'www.google.de': '172.217.20.99', 'www.heise.de': '193.99.144.85', 'www.lanbugs.de': '81.169.181.94', 'www.microsoft.com': '104.108.168.41'}