-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/main' into windows
- Loading branch information
Showing
17 changed files
with
276 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
[bumpversion] | ||
current_version = 0.0.33 | ||
current_version = 0.0.35 | ||
commit = True | ||
tag = True | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = "0.0.33" | ||
__version__ = "0.0.35" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from contextlib import suppress | ||
from importlib import import_module | ||
from typing import List | ||
|
||
from scrapy.exceptions import NotConfigured | ||
from scrapy.extensions.memusage import MemoryUsage | ||
|
||
from scrapy_playwright.handler import ScrapyPlaywrightDownloadHandler, logger | ||
|
||
|
||
_MIB_FACTOR = 1024**2 | ||
|
||
|
||
class ScrapyPlaywrightMemoryUsageExtension(MemoryUsage): | ||
def __init__(self, *args, **kwargs) -> None: | ||
super().__init__(*args, **kwargs) | ||
try: | ||
self.psutil = import_module("psutil") | ||
except ImportError as exc: | ||
raise NotConfigured("The psutil module is not available") from exc | ||
|
||
def _get_main_process_ids(self) -> List[int]: | ||
try: | ||
return [ | ||
handler.playwright_context_manager._connection._transport._proc.pid | ||
for handler in self.crawler.engine.downloader.handlers._handlers.values() | ||
if isinstance(handler, ScrapyPlaywrightDownloadHandler) | ||
and handler.playwright_context_manager | ||
] | ||
except Exception: | ||
return [] | ||
|
||
def _get_descendant_processes(self, process) -> list: | ||
children = process.children() | ||
result = children.copy() | ||
for child in children: | ||
result.extend(self._get_descendant_processes(child)) | ||
return result | ||
|
||
def _get_total_playwright_process_memory(self) -> int: | ||
process_list = [self.psutil.Process(pid) for pid in self._get_main_process_ids()] | ||
for proc in process_list.copy(): | ||
process_list.extend(self._get_descendant_processes(proc)) | ||
total_process_size = 0 | ||
for proc in process_list: | ||
with suppress(Exception): # might fail if the process exited in the meantime | ||
total_process_size += proc.memory_info().rss | ||
logger.debug( | ||
"Total Playwright process memory: %i Bytes (%i MiB)", | ||
total_process_size, | ||
total_process_size / _MIB_FACTOR, | ||
) | ||
return total_process_size | ||
|
||
def get_virtual_size(self) -> int: | ||
return super().get_virtual_size() + self._get_total_playwright_process_memory() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.