Skip to content

Commit

Permalink
Improve PyPy and Windows support (#130)
Browse files Browse the repository at this point in the history
* Improve PyPy and Windows support

* Use platform-appropriate defaults in uvicorn.run

* Explicit encoding when using 'open' in setup.py

* Version 0.2.13

* Fix to setup.py
  • Loading branch information
tomchristie authored Jul 18, 2018
1 parent dd5c2c3 commit c1f1d17
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 15 deletions.
36 changes: 27 additions & 9 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
import re
import sys
import platform

from setuptools import setup

Expand All @@ -12,15 +13,16 @@ def get_version(package):
"""
Return package version as listed in `__version__` in `init.py`.
"""
init_py = open(os.path.join(package, '__init__.py')).read()
path = os.path.join(package, '__init__.py')
init_py = open(path, 'r', encoding='utf8').read()
return re.search("__version__ = ['\"]([^'\"]+)['\"]", init_py).group(1)


def get_long_description():
"""
Return the README.
"""
return open('README.md', 'r').read()
return open('README.md', 'r', encoding='utf8').read()


def get_packages(package):
Expand All @@ -32,6 +34,28 @@ def get_packages(package):
if os.path.exists(os.path.join(dirpath, '__init__.py'))]


if platform.python_implementation() == 'PyPy':
requirements = [
'click',
'h11',
'websockets>=6.0'
]
elif platform.system() == 'Windows' or platform.system().startswith('CYGWIN'):
requirements = [
'click',
'h11',
'websockets>=6.0'
]
else:
requirements = [
'click',
'h11',
'httptools',
'uvloop',
'websockets>=6.0'
]


setup(
name='uvicorn',
version=get_version('uvicorn'),
Expand All @@ -43,13 +67,7 @@ def get_packages(package):
author='Tom Christie',
author_email='[email protected]',
packages=get_packages('uvicorn'),
install_requires=[
'click',
'h11',
'httptools',
'uvloop',
'websockets>=6.0'
],
install_requires=requirements,
classifiers=[
'Development Status :: 3 - Alpha',
'Environment :: Web Environment',
Expand Down
2 changes: 1 addition & 1 deletion uvicorn/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from uvicorn.main import main, run

__version__ = "0.2.12"
__version__ = "0.2.13"
__all__ = ["main", "run"]
26 changes: 21 additions & 5 deletions uvicorn/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import signal
import os
import logging
import platform
import sys


Expand All @@ -22,12 +23,23 @@
HTTP_PROTOCOLS = {"h11": H11Protocol, "httptools": HttpToolsProtocol}


if platform.python_implementation() == 'PyPy':
DEFAULT_LOOP = 'asyncio'
DEFAULT_PARSER = 'h11'
elif platform.system() == 'Windows' or platform.system().startswith('CYGWIN'):
DEFAULT_LOOP = 'asyncio'
DEFAULT_PARSER = 'h11'
else:
DEFAULT_LOOP = 'uvloop'
DEFAULT_PARSER = 'httptools'


@click.command()
@click.argument("app")
@click.option("--host", type=str, default="127.0.0.1", help="Host")
@click.option("--port", type=int, default=8000, help="Port")
@click.option("--loop", type=LOOP_CHOICES, default="uvloop", help="Event loop")
@click.option("--http", type=HTTP_CHOICES, default="httptools", help="HTTP Handler")
@click.option("--loop", type=LOOP_CHOICES, default=DEFAULT_LOOP, help="Event loop")
@click.option("--http", type=HTTP_CHOICES, default=DEFAULT_PARSER, help="HTTP Handler")
@click.option("--workers", type=int, default=1, help="Number of worker processes")
@click.option("--log-level", type=LEVEL_CHOICES, default="info", help="Log level")
def main(app, host: str, port: int, loop: str, http: str, workers: int, log_level: str):
Expand All @@ -54,9 +66,9 @@ def run(app, host="127.0.0.1", port=8000, log_level="info"):
log_level = LOG_LEVELS[log_level]
logging.basicConfig(format="%(levelname)s: %(message)s", level=log_level)

loop = get_event_loop("uvloop")
loop = get_event_loop(DEFAULT_LOOP)
logger = logging.getLogger()
protocol_class = HttpToolsProtocol
protocol_class = {'httptools': HttpToolsProtocol, 'h11': H11Protocol}[DEFAULT_PARSER]

server = Server(app, host, port, loop, logger, protocol_class)
server.run()
Expand Down Expand Up @@ -142,7 +154,11 @@ def run(self):
self.loop.run_forever()

def handle_exit(self, sig, frame):
self.logger.warning("Received signal {}. Shutting down.".format(sig.name))
if hasattr(sig, 'name'):
msg = "Received signal %s. Shutting down." % sig.name
else:
msg = "Received signal. Shutting down."
self.logger.warning(msg)
self.should_exit = True

def create_protocol(self):
Expand Down

0 comments on commit c1f1d17

Please sign in to comment.