Install Winloop via pip
mainTo install Winloop on your Windows operating system, use the standard pip command:
pip install winlooprepository·main·Indexed 42 days ago
https://github.com/vizonex/winloopA 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.
To install Winloop on your Windows operating system, use the standard pip command:
pip install winloopIf 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 --inplaceTo 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())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())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 hereWinloop 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 |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())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()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.The current version of the winloop package is available via the __version__ attribute. You can use this to verify your installation or ensure compatibility with other tools.
import winloop
print(winloop.__version__)