-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
92 lines (73 loc) · 2.83 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# Standard library imports
import time
from asyncio import tasks
import asyncio, requests
from concurrent.futures import ThreadPoolExecutor
from requests.sessions import Session, session
# Local library imports
from core.data import get_world, save_world
from core.forecast import Forecast
from core.info_processor import D1_Handler as data_1
from core.info_processor import D2_Handler as data_2
from core.airports import IATA_handler as iata
start_time = time.time()
API = 'ENTER_YOUR_API_KEY'
f = Forecast(API)
iata_decode = iata()
states = get_world()
async def get_data_iata(places_to_fetch: list, states):
''' This asyncronous func handles the multiple requests called by `Forecast.call_coordinates()`
- Args:
- places_to_fetch (`list`): A `list` containing IATA codes for the function to retrieve its Weather Forecast.
- states: A python `dict` that contains the cache.
'''
with ThreadPoolExecutor(max_workers=10) as executor:
with requests.Session() as session:
loop = asyncio.get_event_loop()
tasks = [
loop.run_in_executor(
executor,
f.call_coordinates,
*(session, coords, states)
)
for coords in places_to_fetch
]
for response in await asyncio.gather(*tasks):
pass
async def get_data_place(places_to_fetch: list, states):
''' This asyncronous func handles the multiple requests called by `Forecast.call_place()`
- Args:
- places_to_fetch (`list`): A `list` containing city names for the function to retrieve its Weather Forecast.
- states: A python `dict` that contains the cache.
'''
with ThreadPoolExecutor(max_workers=10) as executor:
with requests.Session() as session:
loop = asyncio.get_event_loop()
tasks = [
loop.run_in_executor(
executor,
f.call_place,
*(session, coords, states)
)
for coords in places_to_fetch
]
for response in await asyncio.gather(*tasks):
pass
def main():
d1 = data_1('./resources/dataset1.csv')
d1_list = d1.fetch()
d1_coords = d1_list[:20:-1]
d2 = data_2('./resources/dataset2.csv')
d2_list = d2.fetch()
call = d2_list[200:210]
loop = asyncio.get_event_loop()
future = asyncio.ensure_future(get_data_iata(d1_coords, states))
loop.run_until_complete(future)
#save_world(states)
loop = asyncio.get_event_loop()
future = asyncio.ensure_future(get_data_place(call, states))
loop.run_until_complete(future)
save_world(states)
if __name__ == '__main__':
main()
print('--- Completed in %s seconds ---'% (round(time.time() - start_time, 3)))