Skip to content

Commit

Permalink
refactor(*): supersede on-demand run_in_executor with unconditional one
Browse files Browse the repository at this point in the history
Signed-off-by: Rongrong <[email protected]>
  • Loading branch information
Rongronggg9 committed Nov 6, 2023
1 parent 0a8aef0 commit e09cc83
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 32 deletions.
12 changes: 0 additions & 12 deletions src/aio_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,6 @@ def _get_executor():
return chosen_executor


async def run_async_on_demand(func: Callable,
*args,
condition: Union[Callable, bool] = None,
prefer_pool: POOL_TYPE = None,
**kwargs):
return (
await run_async(func, *args, prefer_pool=prefer_pool, **kwargs)
if condition and (condition is True or condition(*args, **kwargs)) else
func(*args, **kwargs)
)


async def run_async(func: Callable, *args, prefer_pool: POOL_TYPE = None, **kwargs):
"""
Run a CPU-consuming function asynchronously.
Expand Down
8 changes: 4 additions & 4 deletions src/command/inner/sub.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from os import path

from ... import db, web, env
from ...aio_helper import run_async_on_demand
from ...aio_helper import run_async
from ...i18n import i18n
from .utils import get_hash, update_interval, list_sub, get_http_last_modified, filter_urls, logger, escape_html, \
check_sub_limit
Expand Down Expand Up @@ -345,9 +345,9 @@ async def feed_sniffer(url: str, html: AnyStr) -> Optional[str]:
# if len(html) < 69: # len of `<html><head></head><body></body></html>` + `<link rel="alternate" href="">`
# return None # too short to sniff

soup = await run_async_on_demand(BeautifulSoup, html, 'lxml',
parse_only=SoupStrainer(name=('a', 'link'), attrs={'href': True}),
prefer_pool='thread', condition=len(html) > 64 * 1024)
soup = await run_async(BeautifulSoup, html, 'lxml',
parse_only=SoupStrainer(name=('a', 'link'), attrs={'href': True}),
prefer_pool='thread')
links = (
soup.find_all(name='link', attrs={'rel': 'alternate', 'type': FeedLinkTypeMatcher})
or
Expand Down
6 changes: 3 additions & 3 deletions src/command/opml.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from .. import env, db
from ..compat import bozo_exception_removal_wrapper
from ..aio_helper import run_async_on_demand
from ..aio_helper import run_async
from ..i18n import i18n
from . import inner
from .utils import command_gatekeeper, logger, send_success_and_failure_msg, get_callback_tail, check_sub_limit
Expand Down Expand Up @@ -81,10 +81,10 @@ async def opml_import(event: Union[events.NewMessage.Event, Message],
reply: Message = await event.reply(i18n[lang]['processing'] + '\n' + i18n[lang]['opml_import_processing'])
logger.info(f'Got an opml file from {chat_id}')

opml_d = await run_async_on_demand(
opml_d = await run_async(
partial(bozo_exception_removal_wrapper,
listparser.parse, opml_file),
condition=len(opml_file) > 64 * 1024
prefer_pool='thread' if len(opml_file) < 64 * 1024 else None
)
if not opml_d.feeds:
await reply.edit('ERROR: ' + i18n[lang]['opml_parse_error'])
Expand Down
5 changes: 2 additions & 3 deletions src/parsing/html_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from .medium import Video, Image, Media, Animation, Audio, UploadedImage
from .html_node import *
from .utils import stripNewline, stripLineEnd, isAbsoluteHttpLink, resolve_relative_link, emojify, is_emoticon
from ..aio_helper import run_async_on_demand
from ..aio_helper import run_async

convert_table_to_png: Optional[Awaitable]
if env.TABLE_TO_IMAGE:
Expand Down Expand Up @@ -55,8 +55,7 @@ def __init__(self, html: str, feed_link: Optional[str] = None):
self._parse_item_count = 0

async def parse(self):
self.soup = await run_async_on_demand(BeautifulSoup, self.html, 'lxml',
prefer_pool='thread', condition=len(self.html) > 64 * 1024)
self.soup = await run_async(BeautifulSoup, self.html, 'lxml', prefer_pool='thread')
self.html_tree = HtmlTree(await self._parse_item(self.soup))
self.parsed = True

Expand Down
5 changes: 2 additions & 3 deletions src/parsing/tgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from .. import env, log
from .utils import is_emoticon, emojify, resolve_relative_link, isAbsoluteHttpLink
from .medium import construct_weserv_url
from ..aio_helper import run_async_on_demand
from ..aio_helper import run_async

convert_table_to_png: Optional[Awaitable]
if env.TABLE_TO_IMAGE:
Expand Down Expand Up @@ -203,8 +203,7 @@ def __init__(self, html: str = None, title: str = None, link: str = None, feed_t
self.task = env.loop.create_task(self.generate_page())

async def generate_page(self):
soup = await run_async_on_demand(BeautifulSoup, self.html, 'lxml',
prefer_pool='thread', condition=len(self.html) > 64 * 1024)
soup = await run_async(BeautifulSoup, self.html, 'lxml', prefer_pool='thread')

for tag in soup.find_all(recursive=True):
with suppress(ValueError, AttributeError):
Expand Down
4 changes: 2 additions & 2 deletions src/parsing/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from os import path

from .. import log
from ..aio_helper import run_async_on_demand
from ..aio_helper import run_async
from ..compat import parsing_utils_html_validator_minify

logger = log.getLogger('RSStT.parsing')
Expand Down Expand Up @@ -141,7 +141,7 @@ def _html_validator(html: str) -> str:


async def html_validator(html: str) -> str:
return await run_async_on_demand(_html_validator, html, condition=len(html) > 64 * 1024)
return await run_async(_html_validator, html, prefer_pool='thread')


def html_space_stripper(s: str, enable_emojify: bool = False) -> str:
Expand Down
9 changes: 4 additions & 5 deletions src/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

from . import env, log, locks
from .compat import bozo_exception_removal_wrapper
from .aio_helper import run_async_on_demand
from .aio_helper import run_async
from .i18n import i18n
from .errors_collection import RetryInIpv4

Expand Down Expand Up @@ -337,11 +337,11 @@ async def feed_get(url: str, timeout: Optional[float] = sentinel, web_semaphore:
return ret

with BytesIO(rss_content) as rss_content_io:
rss_d = await run_async_on_demand(
rss_d = await run_async(
partial(bozo_exception_removal_wrapper,
feedparser.parse, rss_content_io, sanitize_html=False,
response_headers={k.lower(): v for k, v in resp.headers.items()}),
condition=len(rss_content) > 64 * 1024
prefer_pool='thread' if len(rss_content) < 64 * 1024 else None
)

if not rss_d.feed.get('title'): # why there is no feed hospital?
Expand Down Expand Up @@ -494,8 +494,7 @@ async def get_page_title(url: str, allow_hostname=True, allow_path: bool = False
raise ValueError('not an HTML page')
# if len(r.content) <= 27: # len of `<html><head><title></title>`
# raise ValueError('invalid HTML')
soup = await run_async_on_demand(BeautifulSoup, r.content, 'lxml',
prefer_pool='thread', condition=len(r.content) > 64 * 1024)
soup = await run_async(BeautifulSoup, r.content, 'lxml', prefer_pool='thread')
title = soup.title.text
return title.strip()
except Exception:
Expand Down

0 comments on commit e09cc83

Please sign in to comment.