Skip to content

Conversation

@almarklein
Copy link
Member

  • add rendercanvas.get_running_loop() that returns the running loop for the current thread.
  • Instead of sniffio use asyncgen_hooks to detect the current framework.
  • The asyncadapter still registers with sniffio, if sniffio can be imported, to remain compatibility for libs that use sniffio.
  • Some more improvements to the loop lifecycle.

I ran some benchmarks

import sys
import time
import threading
import sniffio

n = 1000000


#

t0 = time.perf_counter()
for _ in range(n):
    try:
        sniffio.current_async_library()
    except sniffio.AsyncLibraryNotFoundError:
        pass
t1 = time.perf_counter()
print(' sniffio:', t1 - t0)


#

def detect_current_async_lib():
    ob = sys.get_asyncgen_hooks()[0]
    if ob is not None:
        try:
            libname = ob.__module__.partition(".")[0]
        except AttributeError:
            return None
        if libname == "rendercanvas":
            libname = "rendercanvas.utils.asyncadapter"
        return libname


t0 = time.perf_counter()
for _ in range(n):
    detect_current_async_lib()
t1 = time.perf_counter()
print('asyncgen:', t1 - t0)

This shows that using asyncgen_hooks is faster. It's speed is also more stable than sniffio, which speed depends on the order it tries different libs.

@almarklein
Copy link
Member Author

I'm so done with these loops ...

@almarklein almarklein changed the title Add concept of a running loop, and drop sniffio Drop sniffio and improved limetime Dec 16, 2025
@almarklein
Copy link
Member Author

Ref pygfx/wgpu-py#780, don't need the idea of a running loop anymore. So this now boils down to dropping sniffio (using async_gen_hooks instead) and some tweaks to improve the definition of when a loop is running.

@almarklein almarklein marked this pull request as ready for review December 16, 2025 20:45
@almarklein almarklein merged commit 863f2bf into main Dec 17, 2025
17 checks passed
@almarklein almarklein deleted the loop-detection branch December 17, 2025 08:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants