def __init__(self, path=None):
        AppServer.__init__(self, path)
        threadCount = self.setting('StartServerThreads')
        self._maxServerThreads = self.setting('MaxServerThreads')
        self._minServerThreads = self.setting('MinServerThreads')
        self._threadPool = []
        self._threadCount = 0
        self._threadUseCounter = []
        # twice the number of threads we have:
        self._requestQueue = Queue.Queue(self._maxServerThreads * 2)
        self._addr = None
        self._requestID = 1

        out = sys.stdout

        out.write('Creating %d threads' % threadCount)
        for i in range(threadCount):
            self.spawnThread()
            out.write(".")
            out.flush()
        out.write("\n")

        self.recordPID()

        self._socketHandlers = {}
        self._handlerCache = {}
        self._sockets = {}

        self.addSocketHandler(self.address(), AdapterHandler)

        self.readyForRequests()
예제 #2
0
	def __init__(self, path=None):
		"""Setup the AppServer.

		Create an initial thread pool (threads created with `spawnThread`),
		and the request queue, record the PID in a file, and add any enabled
		handlers (Adapter, HTTP, Monitor).

		"""
		self._threadPool = []
		self._threadCount = 0
		self._threadUseCounter = []
		self._addr = {}
		self._requestID = 0
		self._socketHandlers = {}
		self._handlerCache = {}
		self._sockets = {}

		self._defaultConfig = None
		AppServer.__init__(self, path)

		try:
			threadCount = self.setting('StartServerThreads')
			self._maxServerThreads = self.setting('MaxServerThreads')
			self._minServerThreads = self.setting('MinServerThreads')
			self._requestQueueSize = self.setting('RequestQueueSize')
			if not self._requestQueueSize:
				# if not set, make queue size twice the max number of threads
				self._requestQueueSize = 2 * self._maxServerThreads
			elif self._requestQueueSize < self._maxServerThreads:
				# otherwise do not make it smaller than the max number of threads
				self._requestQueueSize = self._maxServerThreads
			self._requestBufferSize = self.setting('RequestBufferSize')
			self._responseBufferSize = self.setting('ResponseBufferSize')

			self._requestQueue = Queue.Queue(self._requestQueueSize)

			out = sys.stdout
			out.write('Creating %d threads' % threadCount)
			for i in range(threadCount):
				self.spawnThread()
				out.write(".")
				out.flush()
			out.write("\n")

			if self.setting('EnableAdapter'):
				self.addSocketHandler(AdapterHandler)
			if self.setting('EnableMonitor'):
				self.addSocketHandler(MonitorHandler)
			if self.setting('EnableHTTP'):
				from HTTPServer import HTTPAppServerHandler
				self.addSocketHandler(HTTPAppServerHandler)

			self.readyForRequests()
		except:
			AppServer.initiateShutdown(self)
			raise
예제 #3
0
    def __init__(self, path=None):
        AppServer.__init__(self, path)
        self._requestID = 1
        self.recordPID()
        self._wasd_running = None

        # temporaire
        from WebKit import Profiler
        Profiler.startTime = time()
        self.readyForRequests()
    def __init__(self, path=None):
        AppServer.__init__(self, path)
        self._addr = None
        threadCount = self.setting('StartServerThreads')
        self.maxServerThreads = self.setting('MaxServerThreads')
        self.minServerThreads = self.setting('MinServerThreads')
        self.monitorPort = None
        self.threadPool = []
        self.threadCount = 0
        self.threadUseCounter = []
        self.requestQueue = Queue.Queue(
            self.maxServerThreads * 2)  # twice the number of threads we have
        self.rhCache = []  # This will grow to a limit of the number of
        # threads plus the size of the requestQueue plus one.
        # It used to be a Queue but since we don't make
        # use of the blocking behavior and because of problems
        # with Queue.Empty being raised on .get_nowait()
        # when the queue isn't in fact empty, we have switched
        # to using a list instead.

        self.mainsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        # Must use SO_REUSEADDR to avoid problems restarting the app server
        # This was discussed on Webware-devel in Oct 2001, and this solution
        # was found by Jeff Johnson
        self.mainsocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        addr = self.address()
        try:
            self.mainsocket.bind(addr)
        except:
            if self.running:
                self.initiateShutdown()
            self._closeThread.join()
            raise
        print "Listening on", addr
        open(self.serverSidePath('address.text'),
             'w').write('%s:%d' % (addr[0], addr[1]))
        self.monitorPort = addr[1] - 1

        out = sys.stdout

        out.write('Creating %d threads' % threadCount)
        for i in range(threadCount):
            self.spawnThread()
            out.write(".")
            out.flush()
        out.write("\n")

        # @@ 2001-05-30 ce: another hard coded number:  @@jsl- not everything needs to be configurable....
        self.mainsocket.listen(1024)
        self.recordPID()

        self.readyForRequests()
 def shutDown(self):
     self.running = 0
     self.awakeSelect()
     self._shuttingdown = 1  #jsl-is this used anywhere?
     print "ThreadedAppServer: Shutting Down"
     self.mainsocket.close()
     for i in range(self.threadCount):
         self.requestQueue.put(None)  #kill all threads
     for i in self.threadPool:
         try:
             i.join()
         except:
             pass
     AppServer.shutDown(self)
예제 #6
0
 def defaultConfig(self):
     """The default AppServer.config."""
     if self._defaultConfig is None:
         self._defaultConfig = AppServer.defaultConfig(self).copy()
         # Update with ThreadedAppServer specific settings
         # as defined in defaultConfig on the module level:
         self._defaultConfig.update(defaultConfig)
     return self._defaultConfig
예제 #7
0
	def shutDown(self):
		"""Called on shutdown.

		Also calls `AppServer.shutDown`, but first closes all sockets
		and tells all the threads to die.

		"""
		print "ThreadedAppServer is shutting down..."
		if self._running > 2:
			self._running = 2 # ask main loop to finish
			self.awakeSelect() # unblock select call in mainloop()
			sys.stdout.flush()
			for i in range(30): # wait at most 3 seconds for shutdown
				if self._running < 2:
					break
				time.sleep(0.1)
		if self._sockets:
			# Close all sockets now:
			for sock in self._sockets.values():
				sock.close()
		if self._socketHandlers:
			# Remove the text files with the server addresses:
			for handler in self._socketHandlers.values():
				adrFile = self.addressFileName(handler)
				if os.path.exists(adrFile):
					try:
						os.unlink(adrFile)
					except OSError:
						print "Warning: Could not remove", adrFile
		# Tell all threads to end:
		for i in range(self._threadCount):
			self._requestQueue.put(None)
		for i in self._threadPool:
			try:
				i.join()
			except Exception:
				pass
		# Call super's shutdown:
		AppServer.shutDown(self)
예제 #8
0
 def shutDown(self):
     self._running = 0
     print "CgiPlusAppServer: Shutting Down"
     AppServer.shutDown(self)
예제 #9
0
 def initiateShutdown(self):
     self._wasd_running = False
     AppServer.initiateShutdown(self)
예제 #10
0
    def shutDown(self):
        """Called on shutdown.

        Also calls `AppServer.shutDown`, but first closes all sockets
        and tells all the threads to die.
        """
        print "ThreadedAppServer is shutting down..."
        if self._running > 2:
            self._running = 2  # ask main loop to finish
            self.awakeSelect()  # unblock select call in mainloop()
            sys.stdout.flush()
            for i in range(30):  # wait at most 3 seconds for shutdown
                if self._running < 2:
                    break
                sleep(0.1)
        if self._sockets:
            # Close all sockets now:
            for sock in self._sockets.values():
                sock.close()
        if self._socketHandlers:
            # Remove the text files with the server addresses:
            for handler in self._socketHandlers.values():
                adrFile = self.addressFileName(handler)
                if os.path.exists(adrFile):
                    try:
                        os.unlink(adrFile)
                    except (AttributeError, OSError):
                        print "Warning: Could not remove", adrFile
        # Tell all threads to end:
        for i in range(self._threadCount):
            self._requestQueue.put(None)
        # Join all threads:
        closeTime = time() + 3
        for t in self._threadPool:
            timeout = max(0.1, closeTime - time())
            try:
                t.join(timeout)
            except Exception:
                pass
        # Check whether all threads have ended:
        for t in self._threadPool:
            if t.isAlive():
                if debug:
                    print "Hanging worker thread", t.threadID()
                running = True
                break
        else:
            running = False
        if running:
            # Abort all remaining threads:
            print "Aborting hanging worker threads..."
            for t in self._threadPool:
                if t.isAlive():
                    t.abort(ServerShutDownError)
            # Join remaining threads:
            closeTime = time() + 3
            for t in self._threadPool:
                if t.isAlive():
                    timeout = max(0.1, closeTime - time())
                    try:
                        t.join(timeout)
                    except Exception:
                        pass
            # Check whether remaining threads have ended:
            for t in self._threadPool:
                if t.isAlive():
                    if debug:
                        print "Warning: Could not abort thread", t.threadID()
                    else:
                        print "Warning: Could not abort all worker threads"
                    break
            else:
                print "Hanging worker threads have been aborted."
                running = False
        # Call super's shutdown:
        AppServer.shutDown(self)
예제 #11
0
    def __init__(self, path=None):
        """Setup the AppServer.

        Create an initial thread pool (threads created with `spawnThread`),
        and the request queue, record the PID in a file, and add any enabled
        handlers (Adapter, HTTP, Monitor).
        """
        self._threadPool = []
        self._threadCount = 0
        self._threadUseCounter = []
        self._addr = {}
        self._requestID = 0
        self._socketHandlers = {}
        self._handlerCache = {}
        self._threadHandler = {}
        self._sockets = {}

        self._defaultConfig = None
        AppServer.__init__(self, path)

        try:
            threadCount = self.setting('StartServerThreads')
            self._maxServerThreads = self.setting('MaxServerThreads')
            self._minServerThreads = self.setting('MinServerThreads')
            self._useDaemonThreads = self.setting('UseDaemonThreads')
            self._requestQueueSize = self.setting('RequestQueueSize')
            if not self._requestQueueSize:
                # if not set, make queue size twice the max number of threads
                self._requestQueueSize = 2 * self._maxServerThreads
            elif self._requestQueueSize < self._maxServerThreads:
                # otherwise do not make it smaller than the max number of threads
                self._requestQueueSize = self._maxServerThreads
            self._requestBufferSize = self.setting('RequestBufferSize')
            self._responseBufferSize = self.setting('ResponseBufferSize')

            self._requestQueue = Queue.Queue(self._requestQueueSize)

            maxRequestTime = self.setting('MaxRequestTime') or None
            self._maxRequestTime = maxRequestTime
            self._checkRequestTime = None

            out = sys.stdout
            out.write('Creating %d threads' % threadCount)
            for i in range(threadCount):
                self.spawnThread()
                if not debug:
                    out.write(".")
                out.flush()
            out.write("\n")

            if self.setting('EnableAdapter'):
                self.addSocketHandler(AdapterHandler)
            if self.setting('EnableMonitor'):
                self.addSocketHandler(MonitorHandler)
            if self.setting('EnableSCGI'):
                self.addSocketHandler(SCGIHandler)
            if self.setting('EnableHTTP'):
                from HTTPServer import HTTPAppServerHandler
                self.addSocketHandler(HTTPAppServerHandler)

            self.readyForRequests()

            if maxRequestTime:
                self._checkRequestTime = time() + maxRequestTime
        except:
            AppServer.initiateShutdown(self)
            raise