File tree Expand file tree Collapse file tree 2 files changed +8
-10
lines changed
Expand file tree Collapse file tree 2 files changed +8
-10
lines changed Original file line number Diff line number Diff line change 11#!/usr/bin/env python3
22# -*- coding: utf-8 -*-
33
4+ import threading
45import asyncio
56
67@asyncio .coroutine
78def 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
1213loop = asyncio .get_event_loop ()
13- loop .run_until_complete (hello ())
14+ tasks = [hello (), hello ()]
15+ loop .run_until_complete (asyncio .wait (tasks ))
1416loop .close ()
Original file line number Diff line number Diff line change 11#!/usr/bin/env python3
22# -*- coding: utf-8 -*-
33
4- import threading
54import asyncio
65
76@asyncio .coroutine
87def 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 \n Host: %s\r \n \r \n ' % host
@@ -22,8 +20,6 @@ def wget(host):
2220 writer .close ()
2321
2422loop = 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 ))
2825loop .close ()
29- print ('main thread: %s' % threading .current_thread ())
You can’t perform that action at this time.
0 commit comments