Created
July 25, 2014 16:54
-
-
Save castaneai/acc217ed6bc9b0441f0f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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