def worker(options): """ Start background worker process. """ workerPid = os.getpid() if not options.noaffinity: p = psutil.Process(workerPid) print "affinity [before]", p.cpu_affinity() p.cpu_affinity([options.cpuid]) print "affinity [after]", p.cpu_affinity() factory = EchoServerFactory(options.wsuri) # The master already created the socket, just start listening and accepting ## reactor.adoptStreamPort(options.fd, AF_INET, factory) if not options.silence: print "Worker started on PID %s using factory %s and protocol %s" % (workerPid, factory, factory.protocol) # print "Worker %d PYPYLOG=%s" % (workerPid, os.environ.get('PYPYLOG', None)) if options.profile: statprof.reset(PROFILER_FREQ) statprof.start() if not options.silence: def stat(): if options.profile: statprof.stop() output = StringIO.StringIO() output.write("-" * 80 + "\n") output.write("Worker Statistics (PID %s)\n\n%s" % (workerPid, factory.stats.stats())) if options.profile: output.write("\n") # format = statprof.DisplayFormats.ByLine # format = statprof.DisplayFormats.ByMethod # statprof.display(output, format = format) statprof.display(output) output.write("-" * 80 + "\n\n") sys.stdout.write(output.getvalue()) if options.profile: statprof.reset(PROFILER_FREQ) statprof.start() reactor.callLater(options.interval, stat) reactor.callLater(options.interval, stat) if False: import cProfile print "RUNNING cProfile" cProfile.run('reactor.run()') else: reactor.run()
def run(self): self.factory = HTTPFactory( self.channel_layer, self.action_logger, timeout=self.http_timeout, websocket_timeout=self.websocket_timeout, ping_interval=self.ping_interval, ws_protocols=self.ws_protocols, root_path=self.root_path, ) # Redirect the Twisted log to nowhere globalLogBeginner.beginLoggingTo([lambda _: None], redirectStandardIO=False, discardBuffer=True) # Listen on a socket if self.unix_socket: reactor.listenUNIX(self.unix_socket, self.factory) elif self.file_descriptor: # socket returns the same socket if supplied with a fileno sock = socket.socket(fileno=self.file_descriptor) reactor.adoptStreamPort(self.file_descriptor, sock.family, self.factory) else: reactor.listenTCP(self.port, self.factory, interface=self.host) if "twisted" in self.channel_layer.extensions: logging.info("Using native Twisted mode on channel layer") reactor.callLater(0, self.backend_reader_twisted) else: logging.info("Using busy-loop synchronous mode on channel layer") reactor.callLater(0, self.backend_reader_sync) reactor.callLater(2, self.timeout_checker) reactor.run(installSignalHandlers=self.signal_handlers)
def standalone(): """ Initializes Tornado and our application. Forks worker processes to handle requests. Does not return until all child processes exit normally. """ # Parse arguments parser = argparse.ArgumentParser(description="Homer web server") parser.add_argument("--background", action="store_true", help="Detach and run server in background") parser.add_argument("--worker-processes", default=1, type=int) parser.add_argument("--shared-http-fd", default=None, type=int) parser.add_argument("--process-id", default=0, type=int) args = parser.parse_args() # We don't initialize logging until we fork because we want each child to # have its own logging and it's awkward to reconfigure logging that is # defined by the parent. application = create_application() if args.background: # Get a new logfile, rotating the old one if present. err_log_name = os.path.join(settings.LOGS_DIR, settings.LOG_FILE_PREFIX + "-err.log") try: os.rename(err_log_name, err_log_name + ".old") except OSError: pass # Fork into background. utils.daemonize(err_log_name) # Drop a pidfile. pid = os.getpid() with open(settings.PID_FILE, "w") as pidfile: pidfile.write(str(pid) + "\n") utils.install_sigusr1_handler(settings.LOG_FILE_PREFIX) # Setup logging logging_config.configure_logging(args.process_id) # setup accumulators and counters for statistics gathering api.base.setupStats(args.process_id, args.worker_processes) if args.shared_http_fd: reactor.adoptStreamPort(args.shared_http_fd, AF_INET, application) else: # Cyclone _log.info("Going to listen for HTTP on port %s", settings.HTTP_PORT) http_port = reactor.listenTCP(settings.HTTP_PORT, application, interface=settings.LOCAL_IP) for process_id in range(1, args.worker_processes): reactor.spawnProcess(None, executable, [executable, __file__, "--shared-http-fd", str(http_port.fileno()), "--process-id", str(process_id)], childFDs={0: 0, 1: 1, 2: 2, http_port.fileno(): http_port.fileno()}, env = os.environ) # Kick off the reactor to start listening on configured ports reactor.run()
def run_server(fd=None, port=None, procs=None, verbose=False): if args.verbose: log.startLogging(stdout) environ['SOLEDAD_LOG_TO_STDOUT'] = '1' config = get_config() path = config["blobs_path"] if not port: port = int(config["blobs_port"]) root = Resource() root.putChild('blobs', BlobsResource("filesystem", path)) factory = Site(root) if fd is None: # Create a new listening port and several other # processes to help out. if procs is None: procs = cpu_count() log.msg('A total of %d processes will listen on port %d.' % (procs, port)) port = reactor.listenTCP(port, factory) for i in range(procs - 1): reactor.spawnProcess( None, executable, [executable, __file__, str(port.fileno())], childFDs={0: 0, 1: 1, 2: 2, port.fileno(): port.fileno()}, env=environ) else: # Another process created the port, just start listening on it. log.msg('Adopting file descriptor %d...' % fd) port = reactor.adoptStreamPort(fd, AF_INET, factory) reactor.run()
def child(self, fd): """ Reactor child instance configure """ # Attach to the existing port instance port = reactor.adoptStreamPort( fd, AF_INET, ReceiverFactory() )
def start(self, fd=None): pids = [str(os.getpid())] # script pid if fd is None: # anything in this block is only run once # TODO add global services here, possibly add a services kwarg on # __init__ self.addGlobalServices() self.hendrix.startService() if self.options['workers']: # Create a new listening port and several other processes to help out. childFDs = {0: 0, 1: 1, 2: 2} self.fds = {} for name in self.servers: port = self.hendrix.get_port(name) fd = port.fileno() childFDs[fd] = fd self.fds[name] = fd args = self.getSpawnArgs() transports = [] for i in range(self.options['workers']): transport = reactor.spawnProcess( None, executable, args, childFDs=childFDs, env=environ ) transports.append(transport) pids.append(str(transport.pid)) with open(self.pid, 'w') as pid_file: pid_file.write('\n'.join(pids)) else: # Another process created the port, drop the tcp service and # just start listening on it. fds = pickle.loads(fd) factories = {} for name in self.servers: factory = self.disownService(name) factories[name] = factory self.hendrix.startService() for name, factory in factories.iteritems(): if name == 'main_web_ssl': privateCert = PrivateCertificate.loadPEM( open(self.options['cert']).read() + open(self.options['key']).read() ) factory = TLSMemoryBIOFactory( privateCert.options(), False, factory ) port = reactor.adoptStreamPort(fds[name], AF_INET, factory) reactor.run()
def main(fd=None): resource = WSGIResource(reactor, reactor.getThreadPool(), app) if fd is None: # Create a new listening port and several other processes to help out. port = reactor.listenTCP(8080, Site(resource)) for i in range(3): reactor.spawnProcess( None, executable, [executable, __file__, str(port.fileno())], childFDs={0: 0, 1: 1, 2: 2, port.fileno(): port.fileno()}, env=environ ) else: # Another process created the port, just start listening on it. port = reactor.adoptStreamPort(fd, AF_INET, Site(resource)) reactor.run()
def startService(self): # use socket creation from twisted to use the same options as before if hasattr(self.protocol, 'datagramReceived'): tmp_port = udp.Port(None, None, interface=self.interface) else: tmp_port = tcp.Port(None, None, interface=self.interface) carbon_sock = tmp_port.createInternetSocket() if hasattr(socket, 'SO_REUSEPORT'): carbon_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) carbon_sock.bind((self.interface, self.port)) if hasattr(self.protocol, 'datagramReceived'): self._port = reactor.adoptDatagramPort( carbon_sock.fileno(), socket.AF_INET, self.protocol()) else: carbon_sock.listen(tmp_port.backlog) self._port = reactor.adoptStreamPort( carbon_sock.fileno(), socket.AF_INET, self.factory) carbon_sock.close()
self.ClientNum=0 self.ClientList={} if __name__=='__main__': RootCheck=PasswordCheck() Accessdata=open(r'accessfile.txt','r') data=Accessdata.read() Accessdata.close() login_name=None login_passwd=None while 1: login_name=raw_input("Please enter your login name:") login_passwd=getpass.getpass('Please enter your login passed:') login_Mess=[login_name,data] RootCheck.password=login_passwd if RootCheck.PasswdCheck(login_Mess): break del RootCheck Jsonh=JsonHandle() dbpool=adbapi.ConnectionPool('MySQLdb',db='Nautidoidea',user=login_name,passwd=login_passwd,host='127.0.0.1') log.startLogging(open(r'/Nautiloidea.log'),'w') factory=TCPServerFactory() factory.protocol=TCPServerProtocol print "Server start!" portSocket=socket(AF_INET,SOCK_STREAM) portSocket.setblocking(False) portSocket.bind("192.168.1.107",9990) portSocket.listen(SOMAXCONN) reactor.adoptStreamPort(portSocket.fileno(),AF_INET,factory) portSocket.close() reactor.run()
def standalone(): """ Initializes Tornado and our application. Forks worker processes to handle requests. Does not return until all child processes exit normally. """ # Hack to work-around issue with Cyclone and UNIX domain sockets twisted.internet.address.UNIXAddress.host = "localhost" # Parse arguments parser = argparse.ArgumentParser(description="Crest web server") parser.add_argument("--background", action="store_true", help="Detach and run server in background") parser.add_argument("--signaling-namespace", action="store_true", help="Server running in signaling namespace") parser.add_argument("--worker-processes", default=1, type=int) parser.add_argument("--shared-http-tcp-fd", default=None, type=int) parser.add_argument("--process-id", default=0, type=int) args = parser.parse_args() # Set process name. prctl.prctl(prctl.NAME, settings.PROCESS_NAME) # We don't initialize logging until we fork because we want each child to # have its own logging and it's awkward to reconfigure logging that is # defined by the parent. application = create_application() if args.background: # Get a new logfile, rotating the old one if present. err_log_name = os.path.join(settings.LOGS_DIR, settings.LOG_FILE_PREFIX + "-err.log") try: os.rename(err_log_name, err_log_name + ".old") except OSError: pass # Fork into background. utils.daemonize(err_log_name) utils.install_sigusr1_handler(settings.LOG_FILE_PREFIX) # Setup logging syslog.openlog(settings.LOG_FILE_PREFIX, syslog.LOG_PID) logging_config.configure_logging(settings.LOG_LEVEL, settings.LOGS_DIR, settings.LOG_FILE_PREFIX, args.process_id) twisted.python.log.addObserver(on_twisted_log) pdlogs.CREST_STARTING.log() # setup accumulators and counters for statistics gathering api.base.setupStats(args.process_id, args.worker_processes) # Initialize reactor ports and create worker sub-processes if args.process_id == 0: # Main process startup, create pidfile. # We must keep a reference to the file object here, as this keeps # the file locked and provides extra protection against two processes running at # once. pidfile_lock = None try: pidfile_lock = utils.lock_and_write_pid_file(settings.PID_FILE) # noqa except IOError: # We failed to take the lock - another process is already running exit(1) # Create UNIX domain socket for nginx front-end (used for # normal operation and as a bridge from the default namespace to the signaling # namespace in a multiple interface configuration). bind_safely(reactor, args.process_id, application) pdlogs.CREST_UP.log() if args.signaling_namespace and settings.PROCESS_NAME == "homer": # Running in signaling namespace as Homer, create TCP socket for XDMS requests # from signaling interface _log.info("Going to listen for HTTP on TCP port %s", settings.HTTP_PORT) http_tcp_port = reactor.listenTCP(settings.HTTP_PORT, application, interface=settings.LOCAL_IP) # Spin up worker sub-processes, passing TCP file descriptor for process_id in range(1, args.worker_processes): reactor.spawnProcess(None, executable, [executable, __file__, "--shared-http-tcp-fd", str(http_tcp_port.fileno()), "--process-id", str(process_id)], childFDs={0: 0, 1: 1, 2: 2, http_tcp_port.fileno(): http_tcp_port.fileno()}, env = os.environ) else: # Spin up worker sub-processes for process_id in range(1, args.worker_processes): reactor.spawnProcess(None, executable, [executable, __file__, "--process-id", str(process_id)], childFDs={0: 0, 1: 1, 2: 2}, env = os.environ) else: # Sub-process startup, ensure we die if our parent does. prctl.prctl(prctl.PDEATHSIG, signal.SIGTERM) # Create UNIX domain socket for nginx front-end based on process ID. bind_safely(reactor, args.process_id, application) # Create TCP socket if file descriptor was passed. if args.shared_http_tcp_fd: reactor.adoptStreamPort(args.shared_http_tcp_fd, AF_INET, application) # We need to catch the shutdown request so that we can properly stop # the ZMQ interface; otherwise the reactor won't shut down on a SIGTERM # and will be SIGKILLed when the service is stopped. reactor.addSystemEventTrigger('before', 'shutdown', on_before_shutdown) # Kick off the reactor to start listening on configured ports reactor.run()
def standalone(): """ Initializes Tornado and our application. Forks worker processes to handle requests. Does not return until all child processes exit normally. """ # Hack to work-around issue with Cyclone and UNIX domain sockets twisted.internet.address.UNIXAddress.host = "localhost" # Parse arguments parser = argparse.ArgumentParser(description="Homer web server") parser.add_argument("--background", action="store_true", help="Detach and run server in background") parser.add_argument("--signaling-namespace", action="store_true", help="Server running in signaling namespace") parser.add_argument("--worker-processes", default=1, type=int) parser.add_argument("--shared-http-tcp-fd", default=None, type=int) parser.add_argument("--process-id", default=0, type=int) args = parser.parse_args() # We don't initialize logging until we fork because we want each child to # have its own logging and it's awkward to reconfigure logging that is # defined by the parent. application = create_application() if args.background: # Get a new logfile, rotating the old one if present. err_log_name = os.path.join(settings.LOGS_DIR, settings.LOG_FILE_PREFIX + "-err.log") try: os.rename(err_log_name, err_log_name + ".old") except OSError: pass # Fork into background. utils.daemonize(err_log_name) # Drop a pidfile. pid = os.getpid() with open(settings.PID_FILE, "w") as pidfile: pidfile.write(str(pid) + "\n") utils.install_sigusr1_handler(settings.LOG_FILE_PREFIX) # Setup logging logging_config.configure_logging(settings.LOG_LEVEL, settings.LOGS_DIR, settings.LOG_FILE_PREFIX, args.process_id) # setup accumulators and counters for statistics gathering api.base.setupStats(args.process_id, args.worker_processes) # Initialize reactor ports and create worker sub-processes if args.process_id == 0: # Main process startup, create UNIX domain socket for nginx front-end (used for # normal operation and as a bridge from the default namespace to the signaling # namespace in a multiple interface configuration). _lock_fd = bind_safely(reactor, args.process_id, application) if args.signaling_namespace and settings.PROCESS_NAME == "homer": # Running in signaling namespace as Homer, create TCP socket for XDMS requests # from signaling interface _log.info("Going to listen for HTTP on TCP port %s", settings.HTTP_PORT) http_tcp_port = reactor.listenTCP(settings.HTTP_PORT, application, interface=settings.LOCAL_IP) # Spin up worker sub-processes, passing TCP file descriptor for process_id in range(1, args.worker_processes): reactor.spawnProcess(None, executable, [executable, __file__, "--shared-http-tcp-fd", str(http_tcp_port.fileno()), "--process-id", str(process_id)], childFDs={0: 0, 1: 1, 2: 2, http_tcp_port.fileno(): http_tcp_port.fileno()}, env = os.environ) else: # Spin up worker sub-processes for process_id in range(1, args.worker_processes): reactor.spawnProcess(None, executable, [executable, __file__, "--process-id", str(process_id)], childFDs={0: 0, 1: 1, 2: 2}, env = os.environ) else: # Sub-process startup, create UNIX domain socket for nginx front-end based on # process ID. _lock_fd = bind_safely(reactor, args.process_id, application) # Create TCP socket if file descriptor was passed. if args.shared_http_tcp_fd: reactor.adoptStreamPort(args.shared_http_tcp_fd, AF_INET, application) # Kick off the reactor to start listening on configured ports reactor.run()
def worker(options, socket, factory): workerPid = os.getpid() port = reactor.adoptStreamPort(socket.fileno(), AF_INET, factory) reactor.run()
interface=conf.LISTEN) os.environ['ADOPTED_STREAM'] = str(stream.fileno()) supervisor = GlobalSupervisor() supervisor.setServiceParent(application) heroku_unidling_supervisor = HerokuUnidlingSupervisor() heroku_unidling_supervisor.setServiceParent(application) if conf.MULTIPROCESS > 0: m = MultiProcess(__file__, number=conf.MULTIPROCESS, fds=[stream.fileno()]) m.setServiceParent(application) else: reactor.adoptStreamPort(int(conf.ADOPTED_STREAM), AF_INET, app) metrics_supervisor = MetricsSupervisor() metrics_supervisor.setServiceParent(application) xmpp_keepalive_supervisor = XMPPKeepAliveSupervisor() xmpp_keepalive_supervisor.setServiceParent(application) KatooAPNSService().service.setServiceParent(application) if conf.REDIS_WORKERS > 0: worker.LOGGING_OK_JOBS = conf.LOGGING_OK_JOBS worker.SLEEP_CALL = sleep worker.MAX_RETRIES = conf.BACKEND_MAX_RETRIES worker.MAX_DELAY_TIME = conf.BACKEND_MAX_DELAY
def worker(options): """ Start background worker process. """ workerPid = os.getpid() if not options.noaffinity: p = psutil.Process(workerPid) print "affinity [before]", p.get_cpu_affinity() p.set_cpu_affinity([options.cpuid]) print "affinity [after]", p.get_cpu_affinity() factory = EchoServerFactory(options.wsuri, debug=options.debug) # The master already created the socket, just start listening and accepting ## reactor.adoptStreamPort(options.fd, AF_INET, factory) if not options.silence: print "Worker started on PID %s using factory %s and protocol %s" % ( workerPid, factory, factory.protocol) # print "Worker %d PYPYLOG=%s" % (workerPid, os.environ.get('PYPYLOG', None)) if options.profile: statprof.reset(PROFILER_FREQ) statprof.start() if not options.silence: def stat(): if options.profile: statprof.stop() output = StringIO.StringIO() output.write("-" * 80 + "\n") output.write("Worker Statistics (PID %s)\n\n%s" % (workerPid, factory.stats.stats())) if options.profile: output.write("\n") # format = statprof.DisplayFormats.ByLine # format = statprof.DisplayFormats.ByMethod # statprof.display(output, format = format) statprof.display(output) output.write("-" * 80 + "\n\n") sys.stdout.write(output.getvalue()) if options.profile: statprof.reset(PROFILER_FREQ) statprof.start() reactor.callLater(options.interval, stat) reactor.callLater(options.interval, stat) if False: import cProfile print "RUNNING cProfile" cProfile.run('reactor.run()') else: reactor.run()
def worker(options): """ Start background worker process. """ workerPid = os.getpid() payload = "*" * options.payload if options.resource == 'file': f = open('index.html', 'wb') f.write(payload) f.close() root = static.File('.') elif options.resource == 'data': root = static.Data(payload, 'text/html') elif options.resource == 'fixed': root = FixedResource(payload) else: raise Exception("logic error") if not options.silence: print "Worker started on PID %s using resource %s" % (workerPid, root) if not options.silence: site = CountingSite(root) else: site = Site(root) site.log = lambda _: None # disable any logging ## The master already created the socket, just start listening and accepting ## port = reactor.adoptStreamPort(options.fd, AF_INET, site) if options.profile: statprof.start() if not options.silence: def stat(): if options.profile: statprof.stop() output = StringIO.StringIO() output.write("-" * 80) output.write("\nWorker with PID %s processed %d requests\n" % (workerPid, site.cnt)) if options.profile: output.write("\n") #format = statprof.DisplayFormats.ByLine #format = statprof.DisplayFormats.ByMethod #statprof.display(output, format = format) statprof.display(output) output.write("-" * 80) output.write("\n") output.write("\n") sys.stdout.write(output.getvalue()) if options.profile: statprof.reset() statprof.start() reactor.callLater(options.interval, stat) reactor.callLater(options.interval, stat) reactor.run()
def listenTCPonExistingFD(reactor, fd, factory): return reactor.adoptStreamPort(fd, socket.AF_INET, factory)