Skip to content

Commit

Permalink
ChromeEngine
Browse files Browse the repository at this point in the history
  • Loading branch information
ClericPy committed Oct 9, 2020
1 parent 00c5327 commit 57dc1af
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 53 deletions.
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,55 @@
- Operations on Tabs under stable websocket
- Package very commonly used functions

# What's More?

As we known, `Chrome` browsers (including various webdriver versions) will have the following problems **in a long-running scene**:
1. memory leak
2. missing websocket connections
3. infinitely growing cache
4. other unpredictable problems...

So you may need a more stable process pool scheduling scheme:

```python
import asyncio
from ichrome.pool import ChromeEngine


def test_pool():

async def _test_pool():

tab_callback1 = r'''async def tab_callback(self, tab, url, timeout):
await tab.set_url(url, timeout=5)
return 'Bing' in (await tab.title)'''

async def tab_callback2(self, tab, url, timeout):
await tab.set_url(url, timeout=5)
return 'Bing' in (await tab.title)

async with ChromeEngine(max_concurrent_tabs=5,
headless=True,
disable_image=True) as ce:
tasks = [
asyncio.ensure_future(
ce.do('http://bing.com', tab_callback1, timeout=10))
for _ in range(3)
] + [
asyncio.ensure_future(
ce.do('http://bing.com', tab_callback2, timeout=10))
for _ in range(3)
]
for task in tasks:
assert (await task) is True

# asyncio.run will raise aiohttp issue: https://github.com/aio-libs/aiohttp/issues/4324
asyncio.get_event_loop().run_until_complete(_test_pool())


if __name__ == "__main__":
test_pool()
```

# Install

Expand Down Expand Up @@ -417,5 +465,6 @@ Test Code: [examples_sync.py](https://github.com/ClericPy/ichrome/blob/master/ex
- [x] Add some useful examples.
- [x] Coroutine support (for asyncio).
- [x] Standard test cases.
- [x] Stable Chrome Process Pool.
- [ ] HTTP apis server console [fastapi]. (maybe a new lib)
- [ ] ~~Complete document.~~
36 changes: 34 additions & 2 deletions examples_async.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import asyncio
from typing import List

from ichrome import AsyncChromeDaemon
from ichrome import AsyncChromeDaemon, ChromeEngine
from ichrome.async_utils import Chrome, Tab, Tag, logger

logger.setLevel('DEBUG')
Expand Down Expand Up @@ -327,5 +327,37 @@ def on_shutdown(chromed):
assert AsyncChromeDaemon.bye


def test_chrome_engine():

async def _test_chrome_engine():

tab_callback1 = r'''async def tab_callback(self, tab, url, timeout):
await tab.set_url(url, timeout=5)
return 'Bing' in (await tab.title)'''

async def tab_callback2(self, tab, url, timeout):
await tab.set_url(url, timeout=5)
return 'Bing' in (await tab.title)

async with ChromeEngine(max_concurrent_tabs=5,
headless=True,
disable_image=True) as ce:
tasks = [
asyncio.ensure_future(
ce.do('http://bing.com', tab_callback1, timeout=10))
for _ in range(3)
] + [
asyncio.ensure_future(
ce.do('http://bing.com', tab_callback2, timeout=10))
for _ in range(3)
]
for task in tasks:
assert (await task) is True
print('test_chrome_engine ok')
# asyncio.run will raise aiohttp issue: https://github.com/aio-libs/aiohttp/issues/4324
asyncio.get_event_loop().run_until_complete(_test_chrome_engine())


if __name__ == "__main__":
asyncio.run(test_examples())
test_chrome_engine()
asyncio.get_event_loop().run_until_complete(test_examples())
5 changes: 3 additions & 2 deletions ichrome/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
from .daemon import AsyncChromeDaemon, ChromeDaemon, ChromeWorkers
from .debugger import get_a_tab
from .logs import logger
from .pool import ChromeEngine
from .sync_utils import Chrome, Tab

__version__ = "2.3.9"
__version__ = "2.4.0"
__tips__ = "[github]: https://github.com/ClericPy/ichrome\n[cdp]: https://chromedevtools.github.io/devtools-protocol/\n[cmd args]: https://peter.sh/experiments/chromium-command-line-switches/"
__all__ = [
'Chrome', 'ChromeDaemon', 'Tab', 'Tag', 'AsyncChrome', 'AsyncTab', 'logger',
'AsyncChromeDaemon', 'ChromeWorkers', 'get_a_tab'
'AsyncChromeDaemon', 'ChromeWorkers', 'get_a_tab', 'ChromeEngine'
]
7 changes: 5 additions & 2 deletions ichrome/async_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1550,14 +1550,17 @@ async def get_bounding_client_rect(self,
timeout=timeout)

async def screenshot_element(self,
cssselector: str,
cssselector: str = None,
scale=1,
format: str = 'png',
quality: int = 100,
fromSurface: bool = True,
save_path=None,
timeout=NotSet):
clip = await self.get_element_clip(cssselector, scale=scale)
if cssselector:
clip = await self.get_element_clip(cssselector, scale=scale)
else:
clip = None
return await self.screenshot(format=format,
quality=quality,
clip=clip,
Expand Down
Loading

0 comments on commit 57dc1af

Please sign in to comment.