Skip to content

connection_util

Utility for knowing when it's okay to open the browser.

defer_site_launch_when_available(url_to_wait_for, url_to_launch)

Waits for an URL to become available, then launches the browser.

Source code in client/ayon_comfyui/api/connection_util.py
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
def defer_site_launch_when_available(
    url_to_wait_for: str, url_to_launch: str
) -> None:
    """Waits for an URL to become available, then launches the browser."""

    class BrowserLaunchThread(Thread):
        """Thread used to defer browser execution."""

        def run(self) -> None:
            """Schedule async stuff in sync."""
            loop = asyncio.new_event_loop()
            asyncio.set_event_loop(loop)
            loop.run_until_complete(self.async_run())

        async def async_run(self) -> None:  # noqa: PLR6301
            """Wait for availability of embedded site.

            Then, launch target site.
            """
            await wait_for_site_availability(url_to_wait_for)
            webbrowser.open(url_to_launch)

    BrowserLaunchThread().start()

get_site_headers(url) async

Return headers associated with a site.

Source code in client/ayon_comfyui/api/connection_util.py
75
76
77
78
async def get_site_headers(url: str) -> CIMultiDictProxy:
    """Return headers associated with a site."""
    async with aiohttp.ClientSession() as session, session.head(url) as r:
        return r.headers

poll_site_availability_timeout(url, timeout=5)

Get whether a site is available.

Sync version of wait_for_site_availability_timeout

Returns:

Type Description
bool

True/False whether was able to connect.

Source code in client/ayon_comfyui/api/connection_util.py
60
61
62
63
64
65
66
67
68
69
70
71
72
def poll_site_availability_timeout(url: str, timeout: float = 5) -> bool:
    """Get whether a site is available.

    Sync version of `wait_for_site_availability_timeout`

    Returns:
        True/False whether was able to connect.
    """
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    return loop.run_until_complete(
        wait_for_site_availability_timeout(url, timeout)
    )

poll_site_headers(url)

Return headers associated with a site.

Sync version of get_site_headers

Source code in client/ayon_comfyui/api/connection_util.py
81
82
83
84
85
86
87
88
def poll_site_headers(url: str) -> CIMultiDictProxy:
    """Return headers associated with a site.

    Sync version of `get_site_headers`
    """
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    return loop.run_until_complete(get_site_headers(url))

wait_for_site_availability(url) async

Asynchronously wait for a website to open.

Source code in client/ayon_comfyui/api/connection_util.py
18
19
20
21
22
23
24
25
26
27
28
29
30
31
async def wait_for_site_availability(url: str) -> None:
    """Asynchronously wait for a website to open."""
    async with aiohttp.ClientSession() as session:
        while True:
            try:
                async with session.get(url) as r:
                    # 200 - HTTP OK!
                    if r.status == 200:  # noqa: PLR2004
                        log.info(f"Website @ {url} is up!")  # noqa : G004
                        break
            except aiohttp.ClientConnectorError:
                log.info(f"Website @ {url} not reachable")  # noqa : G004

            await asyncio.sleep(1)

wait_for_site_availability_timeout(url, timeout=5) async

Asynchronously wait for a website to open. with timeout.

Source code in client/ayon_comfyui/api/connection_util.py
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
async def wait_for_site_availability_timeout(
    url: str, timeout: float = 5
) -> None:
    """Asynchronously wait for a website to open. with timeout."""

    async def timeout_(timeout: float) -> None:
        await asyncio.sleep(timeout)

    task_timeout = asyncio.create_task(timeout_(timeout))

    async with aiohttp.ClientSession() as session:
        while not task_timeout.done():
            try:
                async with session.get(url) as r:
                    if r.status == 200:  # noqa: PLR2004
                        # cancel timeout task if still running
                        task_timeout.cancel()
                        return True

            except aiohttp.ClientConnectorError:
                pass

            await asyncio.sleep(0.2)
    return False