mirror of
https://github.com/galaxyproject/galaxy.git
synced 2026-08-31 01:02:04 +08:00
rollback...
This commit is contained in:
@@ -55,8 +55,6 @@ import SocketServer
|
||||
import Queue
|
||||
import threading
|
||||
import socket
|
||||
import os
|
||||
import signal
|
||||
|
||||
import logging
|
||||
log = logging.getLogger( __name__ )
|
||||
@@ -105,17 +103,16 @@ class ThreadPool( object ):
|
||||
for worker in self.workers:
|
||||
worker.join()
|
||||
|
||||
class PreforkThreadPoolServer( SocketServer.TCPServer ):
|
||||
class ThreadPoolServer( SocketServer.TCPServer ):
|
||||
"""
|
||||
Server that uses a pool of threads for request handling
|
||||
"""
|
||||
allow_reuse_address = 1
|
||||
def __init__( self, server_address, request_handler, nworkers, nprocesses ):
|
||||
def __init__( self, server_address, request_handler, nworkers ):
|
||||
# Create and start the workers
|
||||
self.nprocesses = nprocesses
|
||||
self.nworkers = nworkers
|
||||
self.running = True
|
||||
assert nworkers > 0, "ThreadPoolServer must have at least one worker"
|
||||
self.thread_pool = ThreadPool( nworkers, "ThreadPoolServer on %s:%d" % server_address )
|
||||
# Call the base class constructor
|
||||
SocketServer.TCPServer.__init__( self, server_address, request_handler )
|
||||
def process_request( self, request, client_address ):
|
||||
@@ -144,32 +141,8 @@ class PreforkThreadPoolServer( SocketServer.TCPServer ):
|
||||
"""
|
||||
Overrides `serve_forever` to shutdown cleanly.
|
||||
"""
|
||||
log.info( "Serving requests..." )
|
||||
# Pre-fork each child
|
||||
children = []
|
||||
for i in range( self.nprocesses )
|
||||
pid = os.fork()
|
||||
if pid:
|
||||
# We are in the parent process
|
||||
children.append( pid )
|
||||
else:
|
||||
# We are in the child process
|
||||
signal.signal( signal.SIGINT, self.shutdown )
|
||||
self.serve_forever_child()
|
||||
sys.exit( 0 )
|
||||
# Wait
|
||||
try:
|
||||
wait_lock = threading.Lock()
|
||||
wait_lock.acquire()
|
||||
except KeyboardInterrupt:
|
||||
# Cleanup, kill all children
|
||||
for child in children:
|
||||
os.kill( child, signal.SIGINT )
|
||||
|
||||
log.info( "Shutting down..." )
|
||||
def serve_forver_child( self ):
|
||||
thread_pool = ThreadPool( nworkers, "ThreadPoolServer on %s:%d" % server_address )
|
||||
try:
|
||||
log.info( "Serving requests..." )
|
||||
while self.running:
|
||||
try:
|
||||
self.handle_request()
|
||||
@@ -177,9 +150,9 @@ class PreforkThreadPoolServer( SocketServer.TCPServer ):
|
||||
# Timeout is expected, gives interrupts a chance to
|
||||
# propogate, just keep handling
|
||||
pass
|
||||
log.info( "Shutting down..." )
|
||||
finally:
|
||||
self.thread_pool.shutdown()
|
||||
|
||||
def shutdown( self ):
|
||||
"""
|
||||
Finish pending requests and shutdown the server
|
||||
@@ -194,7 +167,7 @@ class PreforkThreadPoolServer( SocketServer.TCPServer ):
|
||||
self.socket.settimeout(1)
|
||||
SocketServer.TCPServer.server_activate(self)
|
||||
|
||||
class WSGIPreforkThreadPoolServer( PreforkThreadPoolServer ):
|
||||
class WSGIThreadPoolServer( ThreadPoolServer ):
|
||||
"""
|
||||
Server that mixes ThreadPoolServer and WSGIHandler
|
||||
"""
|
||||
@@ -211,7 +184,7 @@ class WSGIPreforkThreadPoolServer( PreforkThreadPoolServer ):
|
||||
|
||||
def serve( wsgi_app, global_conf, host="127.0.0.1", port="8080",
|
||||
server_version=None, protocol_version=None, start_loop=True,
|
||||
daemon_threads=None, socket_timeout=None, nworkers=10, nprocesses=2 ):
|
||||
daemon_threads=None, socket_timeout=None, nworkers=10 ):
|
||||
"""
|
||||
Similar to `paste.httpserver.serve` but using the thread pool server
|
||||
"""
|
||||
@@ -224,7 +197,7 @@ def serve( wsgi_app, global_conf, host="127.0.0.1", port="8080",
|
||||
assert protocol_version in ('HTTP/0.9','HTTP/1.0','HTTP/1.1')
|
||||
handler.protocol_version = protocol_version
|
||||
|
||||
server = WSGIThreadPoolServer( wsgi_app, server_address, int( nworkers ), int( nprocesses ) )
|
||||
server = WSGIThreadPoolServer( wsgi_app, server_address, int( nworkers ) )
|
||||
if daemon_threads:
|
||||
server.daemon_threads = daemon_threads
|
||||
if socket_timeout:
|
||||
@@ -239,8 +212,5 @@ def serve( wsgi_app, global_conf, host="127.0.0.1", port="8080",
|
||||
pass
|
||||
return server
|
||||
|
||||
if __name__ == '__main__':
|
||||
from paste.wsgilib import dump_environ
|
||||
serve(dump_environ, server_version="Wombles/1.0",
|
||||
protocol_version="HTTP/1.1", port="8888")
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user