Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Share socket on Windows #456

Merged
merged 3 commits into from
Oct 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion uvicorn/__main__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import uvicorn


if __name__ == "__main__":
uvicorn.main()
13 changes: 12 additions & 1 deletion uvicorn/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import asyncio
import inspect
import logging
import os
import platform
import socket
import ssl
import sys
Expand All @@ -26,8 +28,8 @@
"httptools": "uvicorn.protocols.http.httptools_impl:HttpToolsProtocol",
}
WS_PROTOCOLS = {
"none": None,
"auto": "uvicorn.protocols.websockets.auto:AutoWebSocketsProtocol",
"none": None,
"websockets": "uvicorn.protocols.websockets.websockets_impl:WebSocketProtocol",
"wsproto": "uvicorn.protocols.websockets.wsproto_impl:WSProtocol",
}
Expand Down Expand Up @@ -225,11 +227,20 @@ def bind_socket(self):
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((self.host, self.port))
sock.set_inheritable(True)

if platform.system() == "Windows" and (self.workers > 1 or self.should_reload):
#  We need to explicitly share the socket on Windows.
sock = socket.fromshare(sock.share(os.getpid()))

message = "Uvicorn running on %s://%s:%d (Press CTRL+C to quit)"
protocol_name = "https" if self.is_ssl else "http"
self.logger_instance.info(message % (protocol_name, self.host, self.port))
return sock

@property
def should_reload(self):
return isinstance(self.app, str) and (self.debug or self.reload)

@property
def logger_instance(self):
if self.logger is not None:
Expand Down
4 changes: 2 additions & 2 deletions uvicorn/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
HTTP_CHOICES = click.Choice(HTTP_PROTOCOLS.keys())
WS_CHOICES = click.Choice(WS_PROTOCOLS.keys())
LIFESPAN_CHOICES = click.Choice(LIFESPAN.keys())
LOOP_CHOICES = click.Choice(LOOP_SETUPS.keys())
LOOP_CHOICES = click.Choice([key for key in LOOP_SETUPS.keys() if key != "none"])
INTERFACE_CHOICES = click.Choice(INTERFACES)

HANDLED_SIGNALS = (
Expand Down Expand Up @@ -267,7 +267,7 @@ def run(app, **kwargs):
"auto-reload only works when app is passed as an import string."
)

if isinstance(app, str) and (config.debug or config.reload):
if config.should_reload:
sock = config.bind_socket()
supervisor = StatReload(config)
supervisor.run(server.run, sockets=[sock])
Expand Down
1 change: 1 addition & 0 deletions uvicorn/workers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio

from gunicorn.workers.base import Worker

from uvicorn.config import Config
from uvicorn.main import Server

Expand Down