Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

Ticket #321: server.py.patch

Line 
1 Index: server.py
2 ===================================================================
3 --- server.py   (revision 681)
4 +++ server.py   (working copy)
5 @@ -46,7 +46,10 @@
6  onStopServerList = []
7  onStopThreadList = []
8  
9 +# should be False as long as we want the server to keep running
10 +command = 'start'
11  
12 +
13  def start(initOnly=False, serverClass=None):
14      defaultOn = (cherrypy.config.get("server.environment") == "development")
15      if cherrypy.config.get('autoreload.on', defaultOn):
16 @@ -59,8 +62,23 @@
17              except KeyboardInterrupt:
18                  cherrypy.log("<Ctrl-C> hit: shutting down autoreloader", "HTTP")
19              return
20 -   
21 -    _start(initOnly, serverClass)
22 +    # continually recheck the server command upon call to _stop
23 +    while command != 'stop':
24 +        if initOnly:
25 +            # when started with initOnly, the server should initialize
26 +            # and then return control to the caller - thus we break out
27 +            # of this loop
28 +            _start(initOnly, serverClass)
29 +            break
30 +        elif command == 'suspend':
31 +            try:
32 +                time.sleep(0.2)
33 +                continue
34 +            except KeyboardInterrupt:
35 +                cherrypy.log("<Ctrl-C> hit: shutting down", "HTTP")
36 +                stop()
37 +        elif command == 'start':
38 +            _start(initOnly, serverClass)
39  
40  def _start(initOnly=False, serverClass=None):
41      """
42 @@ -167,10 +185,12 @@
43              # This should block until the http server stops.
44              cherrypy._httpserver.start()
45          except (KeyboardInterrupt, SystemExit):
46 -            pass
47 +            # we want to shutdown the server
48 +            global command
49 +            command = 'stop'
50      finally:
51          cherrypy.log("<Ctrl-C> hit: shutting down", "HTTP")
52 -        stop()
53 +        _stop()
54  
55  
56  seen_threads = {}
57 @@ -217,14 +237,21 @@
58          _cphttptools.Request(clientAddress, remoteHost,
59                               requestLine, headers, rfile, scheme)
60  
61 -def stop():
62 -    """Shutdown CherryPy (and any HTTP servers it started)."""
63 +def _stop():
64 +    """Stop any running http servers.
65 +
66 +    Unless cherrypy.server.command is 'stop' or 'suspend', the server will start
67 +    right up again, thus making this function equivalent to a
68 +    'restart' function.
69 +    """
70      try:
71          httpstop = cherrypy._httpserver.stop
72      except AttributeError:
73          pass
74      else:
75          httpstop()
76 +        # wait for worker threads to stop
77 +        time.sleep(1)
78     
79      # Call the functions from cherrypy.server.onStopThreadList
80      for thread_ident, i in seen_threads.iteritems():
81 @@ -239,16 +266,23 @@
82      cherrypy._httpserver = None
83      cherrypy._appserver_state = 0
84  
85 -def restart():
86 -    """Stop and start CherryPy."""
87 -    http = getattr(cherrypy, '_httpserver', None)
88 -    if http:
89 -        stop()
90 -        # Give HTTP servers time to shut down their thread pools.
91 -        time.sleep(1)
92 -        # Start the server in a new thread
93 -        thread_args = {"serverClass": http.__class__}
94 -        threading.Thread(target=_start, kwargs=thread_args).start()
95 -    else:
96 -        stop()
97 -        _start(initOnly=True)
98 +
99 +# to keep api and behavior compatibility
100 +restart = _stop
101 +
102 +def stop():
103 +    """Shutdown CherryPy (and any HTTP servers it started)."""
104 +    global command
105 +    command = 'stop'
106 +    _stop()
107 +
108 +def suspend():
109 +    global command
110 +    command = 'suspend'
111 +    _stop()
112 +
113 +def resume():
114 +    global command
115 +    command = 'start'
116 +   
117 +   
118 \ No newline at end of file
119

Hosted by WebFaction

Log in as guest/cpguest to create tickets