Python Aiohttp Bilen
-
Merhaba python da AioHttp diye bir modül varmış requestsle saniye başı attığım istekleri asenkron şekilde atabiliyormuş adamın teki 500 isteği 8 saniyede atmış falan acaba nedir nasıl kullanılır bilen var mı ??
import aiohttp import asyncio from bs4 import BeautifulSoup import time async def fetch(session, url): async with session.get(url) as response: return await response.text() async def konular(link): async with aiohttp.ClientSession() as session: await konu_cek(session, 'https://www.tahribat.com{}'.format(link)) async def konu_cek(session,url): async with session.get(url) as response: icerik = await response.text() soup = BeautifulSoup(icerik,'html.parser') isimler = soup.select('tbody > tr') for isim in isimler: print(isim.select('td:nth-child(2) > a')[0].text.strip()) async def kategoriler(soup): soup = BeautifulSoup(soup,'html.parser') ana_kategoriler = soup.findAll('td',attrs={'style':'width:auto'}) for ana_kategori in ana_kategoriler: await konular(ana_kategori.find('a').get('href')) async def main(): async with aiohttp.ClientSession() as session: html = await fetch(session, 'https://www.tahribat.com/Forum') await kategoriler(html) if __name__ == '__main__': start = time.time() loop = asyncio.get_event_loop() loop.run_until_complete(main()) stop = time.time() print(stop-start)
Şöyle saçma sapan bir kod yazdımda test amaçlı 54 isteği 26 saniyede attı halen yavaş kalıyor yardımcı olabilecek var mı ??
-
requests ile attigin istegi threading ile mass cikartabilirsin sikem aiohtetetepeyi.
kaldir 200 thread, hepsi kalkana kadar queue'ye al; hepsi ayaklandiginda bastir requestleri
-
ufacık bir up olsun
-
pythonda döküman bol aslında hocam internette yok mu bu modülü anlatan döküman?
-
-
Şu threading ile yapıyorumda threadler galiba tamamlanmıyor hiç bir zaman o kısmını anlamadım neden olduğunu
-
https://laike9m.com/blog/requests-secret-pool_connections-and-pool_maxsize,89/
https://docs.aiohttp.org/en/stable/client_advanced.html#limiting-connection-pool-size (The default is 100)
from gevent import monkey, pool monkey.patch_all() import requests session = requests.Session() worker_pool = pool.Pool(500) def worker(url): return session.get(url) url_list = ['https://google.com'] for response in worker_pool.imap_unordered(worker, url_list): do_something()
-
Aiohttp python'ın asenkron çalışma yeteneklerinden faydalanıyor. Eskiden bu tür işlemler için kompleks threading işlemleri yapardık. python 3.5 versiyonundan sonra async/await yapısı ile kod yapısını daha temiz tutabiliyoruz.