Skip to content

Instantly share code, notes, and snippets.

@miklevin
Last active July 16, 2019 19:27
Show Gist options
  • Save miklevin/25806f3a47c2c68365e7b6b3bc925ed8 to your computer and use it in GitHub Desktop.
Save miklevin/25806f3a47c2c68365e7b6b3bc925ed8 to your computer and use it in GitHub Desktop.
Don't really want to use Python asyncio but have to? Here's one way.
import asyncio
async def in_the_future(fut):
result = await fut
print("Side-effect: %s" % result)
return result
async def side_effects(url, name):
await asyncio.sleep(1)
msg = "Saving %s.png for %s" % (url, name)
return msg
def job(url, name):
task = asyncio.ensure_future(side_effects(url, name))
task = in_the_future(task)
asyncio.run_coroutine_threadsafe(task, loop)
# Abandon all hope ye who codes synchronously here.
loop = asyncio.get_event_loop()
shots = {
'one' : 'http://foo',
'two' : 'http://bar',
'three': 'http://spam',
'four' : 'http://eggs'
}
for shot in shots:
job(shot, shots[shot])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment