the semantics are different because the python objects don't chain like this utilizing promises. you can utilize the yield foo() semantics in which python performs foo as a coroutine, or you can use the asyncio eventloop to juggle multiple tasks yourself.
the yield foo() semantics make things appear to run inline as linear code but it's important to note that coroutines can pause at the yield line and resume context at the line that called the coroutine. here's some actual code from one of my classes that does ButterflyDNS.
@asyncio.coroutine
def _get_zone_soa(self, zone):
with (yield from self.pool.cursor()) as cur:
yield from cur.execute('''SELECT c.content as zone,
r.created,r.updated,r.data,r.type,r.host,r.priority,r.ttl
FROM record r,
canonical c
WHERE c.content = %(zone)s
AND r.zone=c.domain
AND r.type='SOA'
''', {'zone':zone})
_ = yield from cur.fetchone()
_ = (yield from self._make_dict_list(cur, [_]))[0]
# note, SOA updates go to everyone
return ('org.head.butterflydns.zone.records.get.soa', _)
@wamp.register('org.head.butterflydns.zone.records.send')
def send_records(self, *args, **detail):
for e in args:
print('send_records received args: {}'.format(args))
for k,v in detail.items():
print('send_records: k={} v={}'.format(k,v))
zone = args[0]
if not zone:
return
def process_runner(zone):
# run all of these concurrently
tasks = [
self._get_zone_soa(zone),
self._get_zone_local(zone),
self._get_zone_registrar(zone),
self._get_zone_transfer_acl(zone),
self._get_zone_resourcerecords(zone),
self._get_zone_ns_glue(zone),
]
for f in asyncio.as_completed(tasks):
topic, result = yield from f
self.push_pub(topic, result)
yield from process_runner(zone)
i've only included _get_zone_soa(), but all of the tasks are semantically the same. the wamp call for zone.records.send will run quickly and the browser client will get a success promptly. as each of the tasks complete, they'll publish their results to a crafted uri and vanish. the webpage UI for managing the zone will respond to the publish and update the appropriate block of HTML as it receives each result.