Created
September 26, 2022 09:43
-
-
Save wjurkowlaniec/5cc3f8b9bc0146c56acb58a1980f30ec to your computer and use it in GitHub Desktop.
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
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