MeiK2333 / pyppeteer_stealth

MIT License
245 stars 53 forks source link

Correct way of running forever and break on SIGINT or SIGTERM signals #13

Closed madzohan closed 3 years ago

madzohan commented 3 years ago

Using python 3.9 Tried different ways but have not succeeded yet

running using code

if __name__ == '__main__':
    with suppress(RuntimeError):
        asyncio.run(open_zxcv_session(1))

coro code where I open the page and wait forever

async def open_zxcv_session(delay: float):
    browser = await launch(headless=True)
    page = await browser.newPage()

    await stealth(page)
    await page.goto('URL here')
    while True:
        await asyncio.sleep(delay)
madzohan commented 3 years ago

Ok I figured out the correct way of running coro using this commit https://github.com/crossbario/autobahn-python/commit/ac1dd18e80ded077c26e4be8f62cce06a37ecfeb

import asyncio
import logging
import signal
from contextlib import suppress

logger = logging.getLogger(__name__)

def main(coro):
    loop = asyncio.get_event_loop()

    async def nicely_exit(sig):
        logger.info(f"Shutting down due to {sig}")

        tasks = asyncio.all_tasks()
        for task in tasks:
            # Do not cancel the current task.
            if task is not asyncio.current_task():
                task.cancel()

        def cancel_all_callback(fut):
            try:
                fut.result()
            except asyncio.CancelledError:
                logger.debug("All task cancelled")
            except Exception as e:
                logger.error("Error while shutting down: {exception}", exception=e)
            finally:
                loop.stop()

        fut = asyncio.gather(*tasks)
        fut.add_done_callback(cancel_all_callback)

    loop.add_signal_handler(signal.SIGINT, lambda: asyncio.ensure_future(nicely_exit("SIGINT")))
    loop.add_signal_handler(signal.SIGTERM, lambda: asyncio.ensure_future(nicely_exit("SIGTERM")))
    with suppress(asyncio.exceptions.CancelledError):
        loop.run_until_complete(coro)