Winloop

repository·main·Indexed 42 days ago

https://github.com/vizonex/winloop

A high-performance alternative to the standard Windows asyncio event loop that provides compatibility with the uvloop interface. It is designed to resolve performance issues and SSL problems common in the standard Windows Python asyncio library, offering significant improvements for TCP connections.

Tokens
1.3K
Snippets
9
Records
10
Agent score
54%

What's inside Winloop

  1. Build Winloop from source

    main

    If you wish to build the extension locally, you can clone the repository and run the setup script. Note that you must have Cython and Visual C++ extensions installed to compile the library.

    python setup.py build_ext --inplace
  2. Integrate Winloop with FastAPI and Uvicorn

    main

    To use Winloop with FastAPI, you must install the loop policy before starting the Uvicorn server. You can pass the Winloop event loop directly to the uvicorn.Config object.

    from fastapi import FastAPI
    import winloop
    import uvicorn
    import asyncio
    
    app = FastAPI()
    
    @app.on_event("startup")
    def make_assertion():
        # Verify that Winloop is the active policy
        assert isinstance(asyncio.get_event_loop_policy(), winloop.WinLoopPolicy)
    
    if __name__ == "__main__":
        winloop.install()
        loop = asyncio.get_event_loop()
        config = uvicorn.Config(app=app, port=10000, loop=loop)
        server = uvicorn.Server(config)
        asyncio.run(server.serve())
  3. Use Winloop as a drop-in replacement for uvloop

    main

    You can write platform-agnostic code that uses uvloop on Linux/macOS and winloop on Windows by checking the system platform and using the run function.

    import sys
    import asyncio
    
    async def main():
        # Your async logic here
        pass
    
    if __name__ == "__main__":
        if sys.platform in ('win32', 'cygwin', 'cli'):
            from winloop import run
        else:
            # if we're on apple or linux do this instead
            from uvloop import run
        run(main())
  4. Install the Winloop event loop

    main

    To use Winloop as your default asyncio event loop policy, call winloop.install(). This is typically done at the very beginning of your application's entry point.

    import winloop
    
    if __name__ == "__main__":
        winloop.install()
        # Your asyncio code here
  5. Winloop Performance Benchmarks (TCP Connections)

    main

    Winloop provides significant performance improvements over the standard Windows asyncio event loop policies for TCP connections.

    | Asyncio Event Loop Policy | Time (in Seconds) |
    |---------------------------|--------------------|
    | WinLoopPolicy             | 0.493s             |
    | WindowsProactorEventLoopPolicy | 2.510s             |
    | WindowsSelectorEventLoopPolicy | 2.723s             |
  6. Run coroutines with winloop.run()

    main

    The preferred way to execute an asynchronous entry point using the winloop event loop is by calling winloop.run(). This function manages the lifecycle of the winloop.Loop, including creation, running the provided coroutine, and proper shutdown of tasks and generators. It is compatible with different Python versions (3.10+).

    If you provide a custom loop_factory, it must return a winloop.Loop or a subtype of it.

    import winloop
    import asyncio
    
    async def main():
        print("Running in winloop!")
        await asyncio.sleep(1)
    
    if __name__ == '__main__':
        winloop.run(main())
  7. Create a new event loop with new_event_loop()

    main

    Returns a new instance of winloop.Loop. Note that creating a loop does not automatically set it as the current event loop for the thread; you must use asyncio.set_event_loop() if you intend to use it manually.

    import winloop
    
    loop = winloop.new_event_loop()
  8. Use the winloop.Loop class

    main
    winloop.Loop is the core event loop implementation. It inherits from asyncio.AbstractEventLoop and provides the high-performance loop implementation for Windows. It is the type returned by new_event_loop() and is required when using winloop.run() with a custom loop_factory.