2020-08-11 15:02:42 +02:00
|
|
|
import asyncio, logging, time, aiohttp
|
2020-08-11 14:25:00 +02:00
|
|
|
from datetime import datetime
|
|
|
|
from threading import Thread
|
|
|
|
from typing import Optional, Tuple, Sequence
|
2020-08-11 15:02:42 +02:00
|
|
|
from app import crud
|
|
|
|
from app.database import SessionLocal
|
2020-08-11 14:25:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
logging.basicConfig(format='%(levelname)s:%(message)s', filename='example.log', filemode='w', level=logging.INFO)
|
|
|
|
|
2020-08-11 15:02:42 +02:00
|
|
|
db=SessionLocal()
|
|
|
|
|
2020-08-11 14:25:00 +02:00
|
|
|
URLS = [
|
|
|
|
"https://python.org",
|
|
|
|
"https://google.com",
|
|
|
|
"https://stackoverflow.com",
|
|
|
|
'https://dotya.ml',
|
|
|
|
'https://git.dotya.ml',
|
|
|
|
'https://drone.dotya.ml',
|
|
|
|
'https://netdata.dotya.ml',
|
|
|
|
'https://tew0x00.dotya.ml',
|
|
|
|
'https://speedtest.dotya.ml',
|
|
|
|
'https://testdotya.dotya.ml',
|
|
|
|
"https://ubuntu.com",
|
|
|
|
]
|
2020-08-11 15:02:42 +02:00
|
|
|
URL = crud.get_urls(db=db)
|
2020-08-11 14:25:00 +02:00
|
|
|
|
|
|
|
def start_background_loop(loop: asyncio.AbstractEventLoop) -> None:
|
|
|
|
asyncio.set_event_loop(loop)
|
|
|
|
loop.run_forever()
|
|
|
|
|
|
|
|
|
|
|
|
async def fetch(url: str, session: aiohttp.ClientSession = None) -> Tuple[str, str]:
|
|
|
|
async def _fetch(url: str, session: aiohttp.ClientSession):
|
2020-08-11 15:02:42 +02:00
|
|
|
URL = crud.get_urls(db=db)
|
2020-08-11 14:25:00 +02:00
|
|
|
s = time.time()
|
|
|
|
async with session.head(url) as response:
|
|
|
|
f = time.time() - s
|
|
|
|
m = ('[ %f ]\t<%s>\ttook: %f\t(%s)' %(time.time(),response.status,f,url))
|
|
|
|
print(m)
|
|
|
|
logging.info(m)
|
|
|
|
return url
|
|
|
|
|
|
|
|
|
|
|
|
if session:
|
|
|
|
return await _fetch(url, session)
|
|
|
|
else:
|
|
|
|
async with aiohttp.ClientSession() as session:
|
|
|
|
return await _fetch(url, session)
|
|
|
|
|
|
|
|
async def fetch_urls(loop: asyncio.AbstractEventLoop) -> Sequence[Tuple[str, str]]:
|
|
|
|
async with aiohttp.ClientSession() as session:
|
2020-08-11 15:02:42 +02:00
|
|
|
tasks = [loop.create_task(fetch(url, session)) for url in URL]
|
2020-08-11 14:25:00 +02:00
|
|
|
results = await asyncio.gather(*tasks)
|
|
|
|
return results
|
|
|
|
|
|
|
|
def main() -> None:
|
|
|
|
loop = asyncio.new_event_loop()
|
|
|
|
t = Thread(target=start_background_loop, args=(loop,), daemon=True)
|
|
|
|
t.start()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while True:
|
|
|
|
task = asyncio.run_coroutine_threadsafe(fetch_urls(loop), loop)
|
|
|
|
time.sleep(10)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|