Skip to content

Instantly share code, notes, and snippets.

@wjurkowlaniec
Created September 26, 2022 09:43
Show Gist options
  • Save wjurkowlaniec/5cc3f8b9bc0146c56acb58a1980f30ec to your computer and use it in GitHub Desktop.
Save wjurkowlaniec/5cc3f8b9bc0146c56acb58a1980f30ec to your computer and use it in GitHub Desktop.
import asyncio
import requests
import aiohttp
import threading
lat = 52.52
long = 13.41
# async/await
URL = "https://api.open-meteo.com/v1/forecast?latitude={}&longitude={}&hourly=temperature_2m,relativehumidity_2m,windspeed_10m"
async def get_weather_async(lat: float, long: float) -> None:
url = URL.format(str(lat), str(long))
async with aiohttp.ClientSession() as session:
async with session.get(url) as resp:
print(resp.status)
async def main_async() -> None:
tasks = []
for i in range(100):
tasks.append(get_weather_async(lat+0.3,long+0.1))
await asyncio.gather(
*tasks,
)
def get_weather(lat: float, long: float) -> None:
url = URL.format(str(lat), str(long))
resp = requests.get(url)
print(resp.status_code)
def main() -> None:
threads = []
for i in range(100):
threads.append( threading.Thread(target = get_weather, args=(lat+0.3,long+0.1)))
[thread.start() for thread in threads]
if __name__ == "__main__":
main()
# asyncio.run(main_async())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment