Skip to content

Instantly share code, notes, and snippets.

@onlurking
Created December 26, 2016 22:51
Show Gist options
  • Save onlurking/9f6b8d6a64abbd6952cbe7fe35e5c7ea to your computer and use it in GitHub Desktop.
Save onlurking/9f6b8d6a64abbd6952cbe7fe35e5c7ea to your computer and use it in GitHub Desktop.
import asyncio
from urllib.request import urlopen
async def produce_lines(filename):
try:
with open(filename, 'r') as file:
for line in file:
yield line.rstrip('\n')
await asyncio.sleep(1)
except FileNotFoundError:
with open("poe.txt", 'w', encoding='utf8') as file:
file.write(
(urlopen("http://www.gutenberg.org/dirs/2/1/4/2147/2147-8.txt")
.read()
.decode("iso8859")))
print("Hi, I've downloaded the The Works of Edgar Allan Poe (Volume 1) for you.\nPlease, re-run this program, shall you..?")
async def print_lines(filename):
async for line in produce_lines(filename):
print(line)
def main():
loop = asyncio.get_event_loop()
loop.run_until_complete(print_lines("poe.txt"))
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment