Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 634b923

Browse filesBrowse files
committed
update async sample
1 parent 57c9c08 commit 634b923
Copy full SHA for 634b923

File tree

Expand file treeCollapse file tree

2 files changed

+8
-10
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+8
-10
lines changed
Open diff view settings
Collapse file

‎samples/async/async_hello.py‎

Copy file name to clipboard
+6-4Lines 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()
Collapse file

‎samples/async/async_wget.py‎

Copy file name to clipboardExpand all lines: samples/async/async_wget.py
+2-6Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.