#!/usr/bin/env python3 import asyncio import aiohttp from functools import partial from some_event_based_library import EventNotifier class MyBackend(EventNotifier): @asyncio.coroutine def get_url(self, url, event): rep = yield from aiohttp.request('get', url) text = yield from rep.text() # do stuff self.send_message(to=event.get('from'), body=text) def init(loop): mybackend = MyBackend(loop=loop) mybackend.add_event_handler('event', partial(mybackend.get_url, 'http://python.org')) mybackend.add_event_handler('other_event', partial(mybackend.get_url, 'http://www.python.org')) LOOP = asyncio.get_event_loop() LOOP.call_soon(init, LOOP) LOOP.run_forever()