def inner_run(self, *args, inside_runzip, **options): ''' inner_run does not get run twice with runserver, unlike .handle() ''' self.inside_runzip = inside_runzip self.makemigrations_and_migrate() # initialize browser bot worker in process memory otree.bots.browser.browser_bot_worker = otree.bots.browser.Worker() # silence the lines like: # 2018-01-10 18:51:18,092 - INFO - worker - Listening on channels # http.request, otree.create_session, websocket.connect, # websocket.disconnect, websocket.receive daphne_logger = logging.getLogger('django.channels') original_log_level = daphne_logger.level daphne_logger.level = logging.WARNING endpoints = build_endpoint_description_strings(host=self.addr, port=self.port) application = self.get_application(options) # silence the lines like: # INFO HTTP/2 support not enabled (install the http2 and tls Twisted extras) # INFO Configuring endpoint tcp:port=8000:interface=127.0.0.1 # INFO Listening on TCP address 127.0.0.1:8000 logging.getLogger('daphne.server').level = logging.WARNING # I removed the IPV6 stuff here because its not commonly used yet addr = self.addr # 0.0.0.0 is not a regular IP address, so we can't tell the user # to open their browser to that address if addr == '127.0.0.1': addr = 'localhost' elif addr == '0.0.0.0': addr = '<ip_address>' self.stdout.write((f"Open your browser to http://{addr}:{self.port}/\n" "To quit the server, press Control+C.\n")) try: self.server_cls( application=application, endpoints=endpoints, signal_handlers=not options["use_reloader"], action_logger=self.log_action, http_timeout=self.http_timeout, root_path=getattr(settings, "FORCE_SCRIPT_NAME", "") or "", websocket_handshake_timeout=self.websocket_handshake_timeout, ).run() daphne_logger.debug("Daphne exited") except KeyboardInterrupt: shutdown_message = options.get("shutdown_message", "") if shutdown_message: self.stdout.write(shutdown_message) return
def _create_server(self): endpoints = build_endpoint_description_strings(host=self.host, port=self._port) return Server( application=self.application, endpoints=endpoints, signal_handlers=False, ws_protocols=getattr(settings, "CHANNELS_WS_PROTOCOLS", None), root_path=getattr(settings, "FORCE_SCRIPT_NAME", "") or "", ready_callable=lambda: self.is_ready.set(), )
def inner_run(self, *args, **options): # Maybe they want the wsgi one? if not options.get("use_asgi", True): if hasattr(RunserverCommand, "server_cls"): self.server_cls = RunserverCommand.server_cls return RunserverCommand.inner_run(self, *args, **options) # Run checks self.stdout.write("Performing system checks...\n\n") self.check(display_num_errors=True) self.check_migrations() # Print helpful text quit_command = "CTRL-BREAK" if sys.platform == "win32" else "CONTROL-C" now = datetime.datetime.now().strftime("%B %d, %Y - %X") if six.PY2: now = now.decode(get_system_encoding()) self.stdout.write(now) self.stdout.write( ("Django version %(version)s, using settings %(settings)r\n" "Starting ASGI/Channels version %(channels_version)s development server" " at %(protocol)s://%(addr)s:%(port)s/\n" "Quit the server with %(quit_command)s.\n") % { "version": self.get_version(), "channels_version": __version__, "settings": settings.SETTINGS_MODULE, "protocol": self.protocol, "addr": "[%s]" % self.addr if self._raw_ipv6 else self.addr, "port": self.port, "quit_command": quit_command, }) # Launch server in 'main' thread. Signals are disabled as it's still # actually a subthread under the autoreloader. self.logger.debug("Daphne running, listening on %s:%s", self.addr, self.port) # build the endpoint description string from host/port options endpoints = build_endpoint_description_strings(host=self.addr, port=self.port) try: self.server_cls( application=self.get_application(options), endpoints=endpoints, signal_handlers=not options["use_reloader"], action_logger=self.log_action, http_timeout=self.http_timeout, ws_protocols=getattr(settings, "CHANNELS_WS_PROTOCOLS", None), root_path=getattr(settings, "FORCE_SCRIPT_NAME", "") or "", websocket_handshake_timeout=self.websocket_handshake_timeout, ).run() self.logger.debug("Daphne exited") except KeyboardInterrupt: shutdown_message = options.get("shutdown_message", "") if shutdown_message: self.stdout.write(shutdown_message) return
def inner_run(self, *args, **options): # Maybe they want the wsgi one? if not options.get("use_asgi", True): if hasattr(RunserverCommand, "server_cls"): self.server_cls = RunserverCommand.server_cls return RunserverCommand.inner_run(self, *args, **options) # Run checks self.stdout.write("Performing system checks...\n\n") self.check(display_num_errors=True) self.check_migrations() # Print helpful text quit_command = "CTRL-BREAK" if sys.platform == "win32" else "CONTROL-C" now = datetime.datetime.now().strftime("%B %d, %Y - %X") self.stdout.write(now) self.stdout.write( ( "Django version %(version)s, using settings %(settings)r\n" "Starting ASGI/Channels version %(channels_version)s development server" " at %(protocol)s://%(addr)s:%(port)s/\n" "Quit the server with %(quit_command)s.\n" ) % { "version": self.get_version(), "channels_version": __version__, "settings": settings.SETTINGS_MODULE, "protocol": self.protocol, "addr": "[%s]" % self.addr if self._raw_ipv6 else self.addr, "port": self.port, "quit_command": quit_command, } ) # Launch server in 'main' thread. Signals are disabled as it's still # actually a subthread under the autoreloader. logger.debug("Daphne running, listening on %s:%s", self.addr, self.port) # build the endpoint description string from host/port options endpoints = build_endpoint_description_strings(host=self.addr, port=self.port) try: self.server_cls( application=self.get_application(options), endpoints=endpoints, signal_handlers=not options["use_reloader"], action_logger=self.log_action, http_timeout=self.http_timeout, root_path=getattr(settings, "FORCE_SCRIPT_NAME", "") or "", websocket_handshake_timeout=self.websocket_handshake_timeout, ).run() logger.debug("Daphne exited") except KeyboardInterrupt: shutdown_message = options.get("shutdown_message", "") if shutdown_message: self.stdout.write(shutdown_message) return
def run_asgi(self, *args, **options): # Launch server in 'main' thread. Signals are disabled as it's still # actually a subthread under the autoreloader. logger.debug("Daphne running, listening on %s:%s", self.addr, self.port) # build the endpoint description string from host/port options endpoints = build_endpoint_description_strings(host=self.addr, port=self.port) Server( application=self.get_handler(**options), endpoints=endpoints, signal_handlers=not options["use_reloader"], action_logger=self.log_action, http_timeout=self.http_timeout, root_path=getattr(settings, "FORCE_SCRIPT_NAME", "") or "", ).run() logger.debug("Daphne exited")
def run(self): """Overriding this _just_ for the port bit...!""" try: # Create the server class -- with our fancy multiprocessing variable (note # `self.port.value`) endpoints = build_endpoint_description_strings( host=self.host, port=self.port.value) self.server = Server(application=self.application, endpoints=endpoints, signal_handlers=False, **self.kwargs) # Set up a poller to look for the port reactor.callLater(0.1, self.resolve_port) # Run with setup/teardown self.setup() try: self.server.run() finally: self.teardown() except Exception as e: # Put the error on our queue so the parent gets it self.errors.put((e, traceback.format_exc()))
def _create_server(self): """ Create a daphne server with local thread asyncio event loop and twisted reactor. """ # Reset reactor to use local thread's event loop from twisted.internet import asyncioreactor del sys.modules["twisted.internet.reactor"] try: event_loop = asyncio.get_event_loop() except RuntimeError: event_loop = asyncio.new_event_loop() asyncioreactor.install(event_loop) from twisted.internet import reactor # Create hook to check if main thread communicated with us reactor.callLater(1, self._on_reactor_hook, reactor) application = self.application if self.static_handler: application = self.static_handler(application) endpoints = build_endpoint_description_strings(host=self.host, port=self.port) def ready(): if self.port == 0: self.port = self.daphne.listening_addresses[0][1] self.is_ready.set() return Server( application=application, endpoints=endpoints, signal_handlers=False, root_path=getattr(settings, "FORCE_SCRIPT_NAME", "") or "", ready_callable=ready, reactor=reactor, )
def _run(addr, port, prod): # if True or prod: # channel_layer = get_channel_layer() # else: # channel_layer = channel_layers[DEFAULT_CHANNEL_LAYER] # channel_layer.router.check_default( # http_consumer=ViewConsumer(), # ) # for _ in range(4): # worker = WorkerThread(channel_layer) # worker.daemon = True # worker.start() try: from daphne.server import Server from daphne.endpoints import build_endpoint_description_strings from channels.routing import get_default_application django.setup() # application = django.core.handlers.wsgi.WSGIHandler() print(addr, port) endpoints = build_endpoint_description_strings(host=addr, port=int(port)) server = Server( # channel_layer=channel_layer, get_default_application(), endpoints=endpoints, # host=addr, # port=int(port), signal_handlers=False, action_logger=log_action, http_timeout=60, ) server.run() except KeyboardInterrupt: return
def main(): os.environ.setdefault("DJANGO_SETTINGS_MODULE", "stellaru.settings_release") django.setup() django.core.management.call_command("makemigrations") django.core.management.call_command("migrate") port = find_port() from stellaru import routing as stellaru_app application = StaticFilesWrapper(stellaru_app.application) endpoints = build_endpoint_description_strings( host=DEFAULT_HOST, port=port, unix_socket=None, file_descriptor=None, ) server = Server(application=application, endpoints=endpoints, server_name='Stellaru') print('Starting Stellaru') thread = threading.Thread(target=open_browser, args=(port, )) thread.run() server.run()
root.setLevel(logging.DEBUG) ch = logging.StreamHandler(sys.stdout) ch.setLevel(logging.DEBUG) formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') ch.setFormatter(formatter) root.addHandler(ch) root.info('TC_DEPLOY: Initialising daphne') # Same Command # daphne asgi:application --unix-socket "/tmp/asgi.socket" --http-timeout 29 --proxy-headers --ping-interval 15 # Start Daphne Server( application=asgi.application, endpoints=build_endpoint_description_strings( unix_socket="/tmp/asgi.socket", ), http_timeout=29, ping_interval=15, ping_timeout=30, websocket_timeout=10800, # 3 hours maximum length websocket_connect_timeout=5, application_close_timeout=10, action_logger=None, ws_protocols=None, root_path="", verbosity=2, proxy_forwarded_address_header="X-Forwarded-For", proxy_forwarded_port_header="X-Forwarded-Port", # proxy_forwarded_proto_header="X-Forwarded-Proto", # Not enabled on currently released daphne ).run()