Skip to content

Commit c847ce2

Browse files
committed
[MZ] Added uvloop.EventLoopPolicy()
1 parent c660872 commit c847ce2

File tree

4 files changed

+9
-2
lines changed

4 files changed

+9
-2
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
This small project comprises a simple Python UDP server plus Dockerfile, as well as a (hopefully) useful Makefile and unittests.
66

7-
The server makes use of the `asyncio` library to allow for asynchronous request handling.
7+
The server makes use of the `asyncio` and `uvloop` libraries to allow for fast, asynchronous request handling.
8+
9+
Check out [this blog post](http://magic.io/blog/uvloop-blazing-fast-python-networking/) if you're interested in more performance details.
810

911
## Prerequisites
1012

app.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import logging
55

66
import coloredlogs
7+
import uvloop
78

89
from utils import create_parser
910

@@ -21,6 +22,7 @@
2122
":crossed:": "🤞",
2223
}
2324

25+
2426
# For further reference see https://docs.python.org/3.7/library/asyncio-protocol.html#udp-echo-client
2527
class MessagePrinterServerProtocol(asyncio.BaseProtocol):
2628
def __init__(self, n: int, s: str, r: bool) -> None:
@@ -81,6 +83,8 @@ def main() -> None:
8183
logger.info(f"Starting UDP server listening on: {APP_HOST}:{APP_PORT}")
8284

8385
loop = asyncio.get_event_loop()
86+
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
87+
8488
transport, protocol = loop.run_until_complete(
8589
loop.create_datagram_endpoint(
8690
lambda: MessagePrinterServerProtocol(args.n, args.s, args.r),

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ six==1.15.0
2020
toml==0.10.1
2121
typed-ast==1.4.1
2222
typing-extensions==3.7.4.2
23+
uvloop==0.14.0
2324
wcwidth==0.2.5
2425
zipp==3.1.0

utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def create_parser() -> ArgumentParser:
1717
default="",
1818
type=str,
1919
required=False,
20-
help="Separator between each emoji (str, default: "")",
20+
help="Separator between each emoji (str, default: '')",
2121
)
2222
parser.add_argument(
2323
"--r",

0 commit comments

Comments
 (0)