-
-
Save lkfo415579/4894854c0bd733762c8fc8f0e67fe046 to your computer and use it in GitHub Desktop.
asyncio example (python3 & python2)
This file contains hidden or 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
import asyncio | |
@asyncio.coroutine | |
def factorial(name, number): | |
f = 1 | |
for i in range(2, number + 1): | |
print("Task %s: Compute factorial(%d)..." % (name, i)) | |
yield from asyncio.sleep(1) | |
f *= i | |
print("Task %s completed! factorial(%d) is %d" % (name, number, f)) | |
loop = asyncio.get_event_loop() | |
tasks = [ | |
asyncio.async(factorial("A", 8)), | |
asyncio.async(factorial("B", 3)), | |
asyncio.async(factorial("C", 4))] | |
loop.run_until_complete(asyncio.wait(tasks)) | |
loop.close() |
This file contains hidden or 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
import trollius as asyncio | |
from trollius import From | |
@asyncio.coroutine | |
def factorial(name, number): | |
f = 1 | |
for i in range(2, number + 1): | |
print("Task %s: Compute factorial(%d)..." % (name, i)) | |
yield From(asyncio.sleep(1)) | |
f *= i | |
print("Task %s completed! factorial(%d) is %d" % (name, number, f)) | |
loop = asyncio.get_event_loop() | |
tasks = [ | |
asyncio.async(factorial("A", 8)), | |
asyncio.async(factorial("B", 3)), | |
asyncio.async(factorial("C", 4))] | |
loop.run_until_complete(asyncio.wait(tasks)) | |
loop.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
555555