winloop.loop


Namewinloop.loop JSON
Version 0.0.1 PyPI version JSON
download
home_page
SummaryAn Alternative library for uvloop compatability with windows
upload_time2023-06-09 20:48:50
maintainer
docs_urlNone
authorVizonex
requires_python
licenseMIT
keywords winloop libuv windows cython fast-asyncio uvloop-alternative
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ![Alt text](https://raw.githubusercontent.com/Vizonex/Winloop/main/winloop.png)

# Winloop
An Alternative library for uvloop compatability with windows Because let's face it. Window's python asyncio can be garabage at times. 
I never really liked the fact that I couldn't make anything run faster escpecially when you have fiber internet connections in place. 
It always felt dissapointing when libuv is avalible on windows but doesn't have uvloop compatability. 
So I went ahead and downloaded the uvloop source code and modified the library to be windows compatable. 

"This library was inpired by the MagicStack Team and I take no credit for the original code and I had to modify." - Vizonex 

The differences with uvloop is that forking has been fully disabled and some smaller api calls had to be changed. Subprocesses instead release the gil instead of forking out although I might change that in the future. If handling asynchronous subprocesses becomes a problem to handle...


However there is a perfromance increase of about 5 times vs using the `WindowsSelectorEventLoopPolicy` and `WindowsProactorEventLoopPolicy` which has ssl problems in python 3.9. Winloop is a very good replacement for that as well.





## How to install Winloop on your windows OS 

```
pip install winloop
```

you can also clone the reposity and build the extension yourself by running if you wish to use/built the extension locally 

```
python setup.py build_ext --inplace 
```

This project is still in it's beta phase and may have some sneaky bugs that we didn't catch yet, so if you find find any bugs you can report them to our github repository.



```python
try:
    import aiohttp
    import aiohttp.web
except ImportError:
    skip_tests = True
else:
    skip_tests = False

import asyncio
import unittest
import weakref
import winloop
import sys

class TestAioHTTP(unittest.TestCase):
    def __init__(self, methodName: str = "test_aiohttp_basic_1") -> None:
        super().__init__(methodName)
       

    def setUp(self):
        self.loop = asyncio.get_event_loop()

    def test_aiohttp_basic_1(self):
        PAYLOAD = '<h1>It Works!</h1>' * 10000

        async def on_request(request):
            return aiohttp.web.Response(text=PAYLOAD)

        asyncio.set_event_loop(self.loop)
        app = aiohttp.web.Application()
        app.router.add_get('/', on_request)

        runner = aiohttp.web.AppRunner(app)
        self.loop.run_until_complete(runner.setup())
        site = aiohttp.web.TCPSite(runner, '0.0.0.0', '10000')
        self.loop.run_until_complete(site.start())
        port = site._server.sockets[0].getsockname()[1]

        async def test():
            # Make sure we're using the correct event loop.
            self.assertIs(asyncio.get_event_loop(), self.loop)

            for addr in (('localhost', port),
                         ('127.0.0.1', port)):
                async with aiohttp.ClientSession() as client:
                    async with client.get('http://{}:{}'.format(*addr)) as r:
                        self.assertEqual(r.status, 200)
                        result = await r.text()
                        self.assertEqual(result, PAYLOAD)

        self.loop.run_until_complete(test())
        self.loop.run_until_complete(runner.cleanup())

    def test_aiohttp_graceful_shutdown(self):
        async def websocket_handler(request):
            ws = aiohttp.web.WebSocketResponse()
            await ws.prepare(request)
            request.app['websockets'].add(ws)
            try:
                async for msg in ws:
                    await ws.send_str(msg.data)
            finally:
                request.app['websockets'].discard(ws)
            return ws

        async def on_shutdown(app):
            for ws in set(app['websockets']):
                await ws.close(
                    code=aiohttp.WSCloseCode.GOING_AWAY,
                    message='Server shutdown')

        asyncio.set_event_loop(self.loop)
        app = aiohttp.web.Application()
        app.router.add_get('/', websocket_handler)
        app.on_shutdown.append(on_shutdown)
        app['websockets'] = weakref.WeakSet()

        runner = aiohttp.web.AppRunner(app)
        self.loop.run_until_complete(runner.setup())
        site = aiohttp.web.TCPSite(runner, '0.0.0.0', '10000')
        self.loop.run_until_complete(site.start())
        port = site._server.sockets[0].getsockname()[1]

        async def client():
            async with aiohttp.ClientSession() as client:
                async with client.ws_connect(
                        'http://127.0.0.1:{}'.format(port)) as ws:
                    await ws.send_str("hello")
                    async for msg in ws:
                        assert msg.data == "hello"

        client_task = asyncio.ensure_future(client())

        async def stop():
            await asyncio.sleep(0.1)
            try:
                await asyncio.wait_for(runner.cleanup(), timeout=0.1)
            except Exception as e:
                print(e)
            finally:
                try:
                    client_task.cancel()
                    await client_task
                except asyncio.CancelledError:
                    pass

        self.loop.run_until_complete(stop())



if __name__ == "__main__":
    # print("tesing without winloop")
    # asyncio.DefaultEventLoopPolicy = asyncio.WindowsSelectorEventLoopPolicy
    # asyncio.DefaultEventLoopPolicy = asyncio.WindowsProactorEventLoopPolicy
    unittest.main()
    # Looks like winloop might be 3x faster than the Proctor Event Loop , THAT's A HUGE IMPROVEMENT! 
    print("testing again but with winloop enabled")
    winloop.install()
    unittest.main()
```

The benchmarks for the code above are as follows 

## Benchmarks

### TCP Connections 
-------------------

| Asyncio Event Loop Policy         | Time (in Seconds)     |          
|-----------------------------------|-----------------------|
| WinLoopPolicy                     | 0.493s                |
| WindowsProactorEventLoopPolicy    | 2.510s                |
| WindowsSelectorEventLoopPolicy    | 2.723s                |


That's a massive increase and jump from just TCP alone I'll be posting more benchmarks soon as 
I modify more of the current test suites made by uvloop...


## How to Use Winloop with Fastapi 

This was a cool little script I put together Just to make fastapi that much faster to handle

```python

from fastapi import FastAPI
from fastapi.responses import HTMLResponse
import winloop 
import uvicorn
import asyncio 
import datetime 

app = FastAPI()

@app.on_event("startup")
def make_assertion():
    # Check to make sure that we bypassed the original eventloop Policy....
    assert isinstance(asyncio.get_event_loop_policy(), winloop.WinLoopPolicy)


@app.get("/test")
async def test_get_request():
    return HTMLResponse("<html><body><h1>FAST API WORKS WITH WINLOOP!</h1></body></html>")


# starllete will use asyncio.to_thread() so that this can remain asynchronous 
@app.get("/date")
def test_dynamic_response():
    return str(datetime.datetime.now())


# Although tricky to pass and is not normal, it does in fact work...
if __name__ == "__main__":
    winloop.install()
    # Winloop's eventlooppolicy will be passed to uvicorn after this point...
    loop = asyncio.get_event_loop()
    config = uvicorn.Config(app=app,port=10000,loop=loop)
    server = uvicorn.Server(config)
    asyncio.run(server.serve())
```


## How To Use Winloop When Uvloop is not avalible

```python

# Here's A small Example of using winloop when uvloop is not avalible to us
import sys
import aiohttp
import asyncio 

async def main():
    async with aiohttp.ClientSession("https://httpbin.org") as client:
        async with client.get("/ip") as resp:
            print(await resp.json())

if __name__ == "__main__":
    if sys.platform in ('win32', 'cygwin', 'cli'):
        from winloop import install
    else:
        # if we're on apple or linux do this instead
        from uvloop import install 
    install()
    asyncio.run(main())
  ```







            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "winloop.loop",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "winloop,libuv,windows,cython,fast-asyncio,uvloop-alternative",
    "author": "Vizonex",
    "author_email": "",
    "download_url": "https://files.pythonhosted.org/packages/48/03/f0a8dbb5ea7f74f181b1d723f3bb2bd4b18f92b08b5bd9869877c2af6310/winloop.loop-0.0.1.tar.gz",
    "platform": "Microsoft Windows",
    "description": "![Alt text](https://raw.githubusercontent.com/Vizonex/Winloop/main/winloop.png)\n\n# Winloop\nAn Alternative library for uvloop compatability with windows Because let's face it. Window's python asyncio can be garabage at times. \nI never really liked the fact that I couldn't make anything run faster escpecially when you have fiber internet connections in place. \nIt always felt dissapointing when libuv is avalible on windows but doesn't have uvloop compatability. \nSo I went ahead and downloaded the uvloop source code and modified the library to be windows compatable. \n\n\"This library was inpired by the MagicStack Team and I take no credit for the original code and I had to modify.\" - Vizonex \n\nThe differences with uvloop is that forking has been fully disabled and some smaller api calls had to be changed. Subprocesses instead release the gil instead of forking out although I might change that in the future. If handling asynchronous subprocesses becomes a problem to handle...\n\n\nHowever there is a perfromance increase of about 5 times vs using the `WindowsSelectorEventLoopPolicy` and `WindowsProactorEventLoopPolicy` which has ssl problems in python 3.9. Winloop is a very good replacement for that as well.\n\n\n\n\n\n## How to install Winloop on your windows OS \n\n```\npip install winloop\n```\n\nyou can also clone the reposity and build the extension yourself by running if you wish to use/built the extension locally \n\n```\npython setup.py build_ext --inplace \n```\n\nThis project is still in it's beta phase and may have some sneaky bugs that we didn't catch yet, so if you find find any bugs you can report them to our github repository.\n\n\n\n```python\ntry:\n    import aiohttp\n    import aiohttp.web\nexcept ImportError:\n    skip_tests = True\nelse:\n    skip_tests = False\n\nimport asyncio\nimport unittest\nimport weakref\nimport winloop\nimport sys\n\nclass TestAioHTTP(unittest.TestCase):\n    def __init__(self, methodName: str = \"test_aiohttp_basic_1\") -> None:\n        super().__init__(methodName)\n       \n\n    def setUp(self):\n        self.loop = asyncio.get_event_loop()\n\n    def test_aiohttp_basic_1(self):\n        PAYLOAD = '<h1>It Works!</h1>' * 10000\n\n        async def on_request(request):\n            return aiohttp.web.Response(text=PAYLOAD)\n\n        asyncio.set_event_loop(self.loop)\n        app = aiohttp.web.Application()\n        app.router.add_get('/', on_request)\n\n        runner = aiohttp.web.AppRunner(app)\n        self.loop.run_until_complete(runner.setup())\n        site = aiohttp.web.TCPSite(runner, '0.0.0.0', '10000')\n        self.loop.run_until_complete(site.start())\n        port = site._server.sockets[0].getsockname()[1]\n\n        async def test():\n            # Make sure we're using the correct event loop.\n            self.assertIs(asyncio.get_event_loop(), self.loop)\n\n            for addr in (('localhost', port),\n                         ('127.0.0.1', port)):\n                async with aiohttp.ClientSession() as client:\n                    async with client.get('http://{}:{}'.format(*addr)) as r:\n                        self.assertEqual(r.status, 200)\n                        result = await r.text()\n                        self.assertEqual(result, PAYLOAD)\n\n        self.loop.run_until_complete(test())\n        self.loop.run_until_complete(runner.cleanup())\n\n    def test_aiohttp_graceful_shutdown(self):\n        async def websocket_handler(request):\n            ws = aiohttp.web.WebSocketResponse()\n            await ws.prepare(request)\n            request.app['websockets'].add(ws)\n            try:\n                async for msg in ws:\n                    await ws.send_str(msg.data)\n            finally:\n                request.app['websockets'].discard(ws)\n            return ws\n\n        async def on_shutdown(app):\n            for ws in set(app['websockets']):\n                await ws.close(\n                    code=aiohttp.WSCloseCode.GOING_AWAY,\n                    message='Server shutdown')\n\n        asyncio.set_event_loop(self.loop)\n        app = aiohttp.web.Application()\n        app.router.add_get('/', websocket_handler)\n        app.on_shutdown.append(on_shutdown)\n        app['websockets'] = weakref.WeakSet()\n\n        runner = aiohttp.web.AppRunner(app)\n        self.loop.run_until_complete(runner.setup())\n        site = aiohttp.web.TCPSite(runner, '0.0.0.0', '10000')\n        self.loop.run_until_complete(site.start())\n        port = site._server.sockets[0].getsockname()[1]\n\n        async def client():\n            async with aiohttp.ClientSession() as client:\n                async with client.ws_connect(\n                        'http://127.0.0.1:{}'.format(port)) as ws:\n                    await ws.send_str(\"hello\")\n                    async for msg in ws:\n                        assert msg.data == \"hello\"\n\n        client_task = asyncio.ensure_future(client())\n\n        async def stop():\n            await asyncio.sleep(0.1)\n            try:\n                await asyncio.wait_for(runner.cleanup(), timeout=0.1)\n            except Exception as e:\n                print(e)\n            finally:\n                try:\n                    client_task.cancel()\n                    await client_task\n                except asyncio.CancelledError:\n                    pass\n\n        self.loop.run_until_complete(stop())\n\n\n\nif __name__ == \"__main__\":\n    # print(\"tesing without winloop\")\n    # asyncio.DefaultEventLoopPolicy = asyncio.WindowsSelectorEventLoopPolicy\n    # asyncio.DefaultEventLoopPolicy = asyncio.WindowsProactorEventLoopPolicy\n    unittest.main()\n    # Looks like winloop might be 3x faster than the Proctor Event Loop , THAT's A HUGE IMPROVEMENT! \n    print(\"testing again but with winloop enabled\")\n    winloop.install()\n    unittest.main()\n```\n\nThe benchmarks for the code above are as follows \n\n## Benchmarks\n\n### TCP Connections \n-------------------\n\n| Asyncio Event Loop Policy         | Time (in Seconds)     |          \n|-----------------------------------|-----------------------|\n| WinLoopPolicy                     | 0.493s                |\n| WindowsProactorEventLoopPolicy    | 2.510s                |\n| WindowsSelectorEventLoopPolicy    | 2.723s                |\n\n\nThat's a massive increase and jump from just TCP alone I'll be posting more benchmarks soon as \nI modify more of the current test suites made by uvloop...\n\n\n## How to Use Winloop with Fastapi \n\nThis was a cool little script I put together Just to make fastapi that much faster to handle\n\n```python\n\nfrom fastapi import FastAPI\nfrom fastapi.responses import HTMLResponse\nimport winloop \nimport uvicorn\nimport asyncio \nimport datetime \n\napp = FastAPI()\n\n@app.on_event(\"startup\")\ndef make_assertion():\n    # Check to make sure that we bypassed the original eventloop Policy....\n    assert isinstance(asyncio.get_event_loop_policy(), winloop.WinLoopPolicy)\n\n\n@app.get(\"/test\")\nasync def test_get_request():\n    return HTMLResponse(\"<html><body><h1>FAST API WORKS WITH WINLOOP!</h1></body></html>\")\n\n\n# starllete will use asyncio.to_thread() so that this can remain asynchronous \n@app.get(\"/date\")\ndef test_dynamic_response():\n    return str(datetime.datetime.now())\n\n\n# Although tricky to pass and is not normal, it does in fact work...\nif __name__ == \"__main__\":\n    winloop.install()\n    # Winloop's eventlooppolicy will be passed to uvicorn after this point...\n    loop = asyncio.get_event_loop()\n    config = uvicorn.Config(app=app,port=10000,loop=loop)\n    server = uvicorn.Server(config)\n    asyncio.run(server.serve())\n```\n\n\n## How To Use Winloop When Uvloop is not avalible\n\n```python\n\n# Here's A small Example of using winloop when uvloop is not avalible to us\nimport sys\nimport aiohttp\nimport asyncio \n\nasync def main():\n    async with aiohttp.ClientSession(\"https://httpbin.org\") as client:\n        async with client.get(\"/ip\") as resp:\n            print(await resp.json())\n\nif __name__ == \"__main__\":\n    if sys.platform in ('win32', 'cygwin', 'cli'):\n        from winloop import install\n    else:\n        # if we're on apple or linux do this instead\n        from uvloop import install \n    install()\n    asyncio.run(main())\n  ```\n\n\n\n\n\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "An Alternative library for uvloop compatability with windows",
    "version": "0.0.1",
    "project_urls": null,
    "split_keywords": [
        "winloop",
        "libuv",
        "windows",
        "cython",
        "fast-asyncio",
        "uvloop-alternative"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "2950ec181d12ead2d8d576dde130fe4c00c773cba08c6e393ad87b12965f764a",
                "md5": "b489d66c7c8c6cbbd3305e5b2cd91acd",
                "sha256": "bfce8fbc3fc5ebc01fafc6d49b9911edf2b4533156bbe444208d2793f88f03eb"
            },
            "downloads": -1,
            "filename": "winloop.loop-0.0.1-cp39-cp39-win_amd64.whl",
            "has_sig": false,
            "md5_digest": "b489d66c7c8c6cbbd3305e5b2cd91acd",
            "packagetype": "bdist_wheel",
            "python_version": "cp39",
            "requires_python": null,
            "size": 495962,
            "upload_time": "2023-06-09T20:48:47",
            "upload_time_iso_8601": "2023-06-09T20:48:47.745352Z",
            "url": "https://files.pythonhosted.org/packages/29/50/ec181d12ead2d8d576dde130fe4c00c773cba08c6e393ad87b12965f764a/winloop.loop-0.0.1-cp39-cp39-win_amd64.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "4803f0a8dbb5ea7f74f181b1d723f3bb2bd4b18f92b08b5bd9869877c2af6310",
                "md5": "1e7bd0ef3ee9ecb7e68d03b9513b6b55",
                "sha256": "839ddffadbd5d9473671def478349897a58bc5d05165c47a2bbeb149b64e5f26"
            },
            "downloads": -1,
            "filename": "winloop.loop-0.0.1.tar.gz",
            "has_sig": false,
            "md5_digest": "1e7bd0ef3ee9ecb7e68d03b9513b6b55",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 706591,
            "upload_time": "2023-06-09T20:48:50",
            "upload_time_iso_8601": "2023-06-09T20:48:50.419664Z",
            "url": "https://files.pythonhosted.org/packages/48/03/f0a8dbb5ea7f74f181b1d723f3bb2bd4b18f92b08b5bd9869877c2af6310/winloop.loop-0.0.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-06-09 20:48:50",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "winloop.loop"
}
        
Elapsed time: 0.09743s