Skip to content

Instantly share code, notes, and snippets.

@NeuronQ
Created February 5, 2019 23:57
Show Gist options
  • Save NeuronQ/c9182b2e7e6b9a27908d4f181f7dcc03 to your computer and use it in GitHub Desktop.
Save NeuronQ/c9182b2e7e6b9a27908d4f181f7dcc03 to your computer and use it in GitHub Desktop.
# async_scrape.py (requires Python 3.7+)
import asyncio, random, time
async def fetch_url(url):
print(f"~ executing fetch_url({url})")
t = time.perf_counter()
await asyncio.sleep(random.randint(1, 5))
print(f"time of fetch_url({url}): {time.perf_counter() - t:.2f}s")
return f"<em>fake</em> page html for {url}"
async def analyze_sentiment(html):
print(f"~ executing analyze_sentiment('{html}')")
t = time.perf_counter()
await asyncio.sleep(random.randint(1, 5))
r = {"positive": random.uniform(0, 1)}
print(f"time of analyze_sentiment('{html}'): {time.perf_counter() - t:.2f}s")
return r
# ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment