forked from jackfrued/Python-100-Days
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
397 additions
and
23 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import asyncio | ||
|
||
|
||
@asyncio.coroutine | ||
def countdown(name, num): | ||
while num > 0: | ||
print(f'Countdown[{name}]: {num}') | ||
yield from asyncio.sleep(1) | ||
num -= 1 | ||
|
||
|
||
def main(): | ||
loop = asyncio.get_event_loop() | ||
tasks = [ | ||
countdown("A", 10), countdown("B", 5), | ||
] | ||
loop.run_until_complete(asyncio.wait(tasks)) | ||
loop.close() | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import asyncio | ||
import aiohttp | ||
|
||
|
||
async def download(url): | ||
print('Fetch:', url) | ||
async with aiohttp.ClientSession() as session: | ||
async with session.get(url) as resp: | ||
print(url, '--->', resp.status) | ||
print(url, '--->', resp.cookies) | ||
print('\n\n', await resp.text()) | ||
|
||
|
||
def main(): | ||
loop = asyncio.get_event_loop() | ||
urls = [ | ||
'https://www.baidu.com', | ||
'http://www.sohu.com/', | ||
'http://www.sina.com.cn/', | ||
'https://www.taobao.com/', | ||
'https://www.jd.com/' | ||
] | ||
tasks = [download(url) for url in urls] | ||
loop.run_until_complete(asyncio.wait(tasks)) | ||
loop.close() | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from time import sleep | ||
|
||
|
||
def countdown_gen(n, consumer): | ||
consumer.send(None) | ||
while n > 0: | ||
consumer.send(n) | ||
n -= 1 | ||
consumer.send(None) | ||
|
||
|
||
def countdown_con(): | ||
while True: | ||
n = yield | ||
if n: | ||
print(f'Countdown {n}') | ||
sleep(1) | ||
else: | ||
print('Countdown Over!') | ||
|
||
|
||
def main(): | ||
countdown_gen(5, countdown_con()) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
Oops, something went wrong.