Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 634b923

Browse files
committed
update async sample
1 parent 57c9c08 commit 634b923

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

samples/async/async_hello.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

4+
import threading
45
import asyncio
56

67
@asyncio.coroutine
78
def hello():
8-
print("Hello world!")
9-
r = yield from asyncio.sleep(1)
10-
print("Hello again!")
9+
print('Hello world! (%s)' % threading.currentThread())
10+
yield from asyncio.sleep(1)
11+
print('Hello again! (%s)' % threading.currentThread())
1112

1213
loop = asyncio.get_event_loop()
13-
loop.run_until_complete(hello())
14+
tasks = [hello(), hello()]
15+
loop.run_until_complete(asyncio.wait(tasks))
1416
loop.close()

samples/async/async_wget.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

4-
import threading
54
import asyncio
65

76
@asyncio.coroutine
87
def wget(host):
98
print('wget %s...' % host)
10-
print('current thread: %s' % threading.current_thread())
119
connect = asyncio.open_connection(host, 80)
1210
reader, writer = yield from connect
1311
header = 'GET / HTTP/1.0\r\nHost: %s\r\n\r\n' % host
@@ -22,8 +20,6 @@ def wget(host):
2220
writer.close()
2321

2422
loop = asyncio.get_event_loop()
25-
tasks = [asyncio.async(wget(host)) for host in ['www.sina.com.cn', 'www.sohu.com', 'www.163.com']]
26-
for task in tasks:
27-
loop.run_until_complete(task)
23+
tasks = [wget(host) for host in ['www.sina.com.cn', 'www.sohu.com', 'www.163.com']]
24+
loop.run_until_complete(asyncio.wait(tasks))
2825
loop.close()
29-
print('main thread: %s' % threading.current_thread())

0 commit comments

Comments
 (0)