Skip to content

Commit ecf9850

Browse files
committed
add async examples
1 parent 3d01723 commit ecf9850

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

python-async/async_example.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import time
2+
import asyncio
3+
4+
async def hellworld(sleep_time):
5+
print('hellworld starts, then sleep for %s seconds' % sleep_time)
6+
await asyncio.sleep(sleep_time)
7+
print('hellworld ends, after sleep: %s seconds' % sleep_time)
8+
9+
loop = asyncio.get_event_loop()
10+
11+
start = time.time()
12+
loop.run_until_complete(asyncio.gather(hellworld(1), hellworld(2)))
13+
print("Running program takes: %s seconds" % (time.time() - start))

python-async/sync_example.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import time
2+
3+
def hellworld(sleep_time):
4+
print('hellworld starts, then sleep for %s seconds' % sleep_time)
5+
time.sleep(sleep_time)
6+
print('hellworld ends, after sleep: %s seconds' % sleep_time)
7+
8+
start = time.time()
9+
hellworld(1)
10+
hellworld(2)
11+
print("Running program takes: %s seconds" % (time.time() - start))

0 commit comments

Comments
 (0)