// html_linear.py import urllib.request for source in ['http://google.com','http://www.ix.de','http://twitter.com']: with urllib.request.urlopen(source) as f: data = f.read() print(data[:40]) // html_process.py from multiprocessing import Process import urllib.request def get_html(source): with urllib.request.urlopen(source) as f: data = f.read() print(data[:40]) if __name__ == '__main__': t1 = Process(target=get_html,args=('http://google.com',)) t1.start() t2 = Process(target=get_html,args=('http://www.ix.de',)) t2.start() t3 = Process(target=get_html,args=('http://twitter.com',)) t3.start() t1.join() t2.join() t3.join() // compute.py # Vergleiche Python-Dokumentation https://docs.python.org/3/library/asyncio-task.html import asyncio async def compute(x, y): print("Rechne {} + {}".format(x, y)) await asyncio.sleep(1.0) return x + y async def print_sum(x, y): result = await compute(x, y) print("{} + {} = {}".format(x, y, result)) loop = asyncio.get_event_loop() loop.run_until_complete(print_sum(1, 2)) loop.close() // html_asynch.py import asyncio import aiohttp async def fetch_page(url): async with aiohttp.ClientSession() as session: async with session.get(url) as response: assert response.status == 200 text = await response.text() print('url: {0}: Content: {1}'.format(url,text[:40])) loop = asyncio.get_event_loop() tasks = [] for i in range(400): tasks.append(asyncio.Task(fetch_page('http://google.com'))) loop.run_until_complete(asyncio.wait(tasks)) loop.close() // future.py import asyncio async def compute1(future): await asyncio.sleep(2) future.set_result(5) async def compute2(future): await asyncio.sleep(1) future.set_result(4) sum = 0 def got_result(future): global sum if sum > 0: sum += future.result() print(sum) loop.stop() else: sum = future.result() if __name__ == '__main__': loop = asyncio.get_event_loop() future1 = asyncio.Future() future2 = asyncio.Future() asyncio.ensure_future(compute1(future1)) future1.add_done_callback(got_result) asyncio.ensure_future(compute2(future2)) future2.add_done_callback(got_result) try: loop.run_forever() finally: loop.close()