Skip to content

Instantly share code, notes, and snippets.

@castaneai
Created July 25, 2014 16:54
Show Gist options
  • Save castaneai/acc217ed6bc9b0441f0f to your computer and use it in GitHub Desktop.
Save castaneai/acc217ed6bc9b0441f0f to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
import asyncio
# coroutineデコレータをつけるだけでコルーチンになる
@asyncio.coroutine
def cor():
asyncio.sleep(1)
return "cor_result"
# Taskオブジェクトを作る
# TaskはFutureのサブクラスで,Taskとキャンセル次の動作が少し異なるぐらいでほぼ同じである
# Taskのコンストラクタではなく,asyncio.async()で作ることが推奨されている
task = asyncio.async(cor())
print(task) # => Task(<coro>)<PENDING>
loop = asyncio.get_event_loop()
loop.run_until_complete(task)
print(task) # => Task(<coro>)<result='cor_result'>
print(loop.call_soon(lambda: print("a")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment