bug: fix caching, and update references to MinerFactory() to miner_factory

This commit is contained in:
UpstreamData
2023-06-12 10:14:20 -06:00
parent 44d6e4cdd4
commit 708d48dcc7
2 changed files with 14 additions and 7 deletions

View File

@@ -365,9 +365,15 @@ class MinerFactory:
self.cache = {} self.cache = {}
async def get_multiple_miners(self, ips: List[str], limit: int = 200): async def get_multiple_miners(self, ips: List[str], limit: int = 200):
tasks = []
results = [] results = []
async for miner in self.get_miner_generator(ips, limit):
results.append(miner)
return results
async def get_miner_generator(self, ips: list, limit: int = 200):
tasks = []
semaphore = asyncio.Semaphore(limit) semaphore = asyncio.Semaphore(limit)
for ip in ips: for ip in ips:
@@ -378,13 +384,14 @@ class MinerFactory:
try: try:
result = await task result = await task
if result is not None: if result is not None:
results.append(result) yield result
finally: finally:
semaphore.release() semaphore.release()
return results
async def get_miner(self, ip: str): async def get_miner(self, ip: str):
ip = str(ip)
if ip in self.cache:
return self.cache[ip]
miner_type = None miner_type = None
for _ in range(RETRIES): for _ in range(RETRIES):
task = asyncio.create_task(self._get_miner_type(ip)) task = asyncio.create_task(self._get_miner_type(ip))

View File

@@ -19,7 +19,7 @@ import ipaddress
import logging import logging
from typing import AsyncIterator, List, Union from typing import AsyncIterator, List, Union
from pyasic.miners.miner_factory import AnyMiner, MinerFactory from pyasic.miners.miner_factory import AnyMiner, miner_factory
from pyasic.network.net_range import MinerNetworkRange from pyasic.network.net_range import MinerNetworkRange
from pyasic.settings import PyasicSettings from pyasic.settings import PyasicSettings
@@ -106,7 +106,7 @@ class MinerNetwork:
logging.debug(f"{self} - (Scan Network For Miners) - Scanning") logging.debug(f"{self} - (Scan Network For Miners) - Scanning")
# clear cached miners # clear cached miners
MinerFactory().clear_cached_miners() miner_factory.clear_cached_miners()
limit = asyncio.Semaphore(PyasicSettings().network_scan_threads) limit = asyncio.Semaphore(PyasicSettings().network_scan_threads)
miners = await asyncio.gather( miners = await asyncio.gather(
@@ -232,7 +232,7 @@ async def ping_and_get_miner(
# make sure the writer is closed # make sure the writer is closed
await writer.wait_closed() await writer.wait_closed()
# ping was successful # ping was successful
return await MinerFactory().get_miner(ip) return await miner_factory.get_miner(ip)
except asyncio.exceptions.TimeoutError: except asyncio.exceptions.TimeoutError:
# ping failed if we time out # ping failed if we time out
continue continue