0

For example, I'm writing a basic wsgi server program.

This program runs perfectly but the server will keeping running until I press Ctrl-C. I want the program to exit when the server has been running for 10 minutes.

And I've tried create a process to exit the program, but I think that's not required, and it doesn't work:

import time
import sys
from multiprocessing import Process
from wsgiref.simple_server import make_server
def application(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/html')])
    return [b'Hello']

def p_exit():
    time.sleep(600) 
    sys.exit()


p = Process(target=p_exit)
p.start()

httpd = make_server('', 80, application)
httpd.serve_forever()
Remi Guan
  • 21,506
  • 17
  • 64
  • 87
  • @JeffC Well, I've tried create a process to exit the program...but I think that isn't required. – Remi Guan Sep 20 '15 at 02:28
  • 1
    As you did in school... please show your work. :) It's part of the process of getting questions answered on SO. It's helpful to you because it forces you to investigate your own problem and think it through. It also proves to readers that you did your homework and made a reasonable attempt to answer your own question. Thirdly, it helps readers find and diagnose the problem resulting in a better answer for you and less time wasted for us. – JeffC Sep 20 '15 at 02:30
  • 1
    @JeffC OK, thanks :). Let me edit my question. – Remi Guan Sep 20 '15 at 02:31
  • @JeffC I've just edited my question. However, this solution is very complicated and it doesn't work. – Remi Guan Sep 20 '15 at 02:40
  • If you use an asynchronous programming framework, like `asyncio` and `tornado`, this wont be a problem. – Huazuo Gao Sep 20 '15 at 02:46
  • What does it do? Does it ever exit? – JeffC Sep 20 '15 at 02:46
  • @JeffC No, it do nothing. – Remi Guan Sep 20 '15 at 02:46
  • @HuazuoGao _asynchronous programming framework_? Let me Google about it first :) – Remi Guan Sep 20 '15 at 02:47
  • Did you try sys.exit(0)? – qwertyuip9 Sep 20 '15 at 05:22
  • @qwertyuip9 really need that? Let me thy it :) – Remi Guan Sep 20 '15 at 05:22
  • @qwertyuip9 still doesn't work. I think that it only exit the new process and it not exit the main process...maybe I should use a thread instead process? – Remi Guan Sep 20 '15 at 05:25
  • 1
    Hmm, I'm not sure although in the documentation, it says "Since exit() ultimately “only” raises an exception, it will only exit the process when called from the main thread, and the exception is not intercepted." – qwertyuip9 Sep 20 '15 at 05:26
  • @qwertyuip9 Well, so any ideas? – Remi Guan Sep 20 '15 at 05:28

1 Answers1

2

You can set an alarm signal handler to exit, and trigger the alarm in 10 minutes:

def handler(*args):
    sys.exit(1)

signal.signal(signal.SIGALRM, handler)
signal.alarm(60*10)

# Now start the wsgi application and wait
httpd = make_server('', 80, application)
httpd.serve_forever()

However, ALRM signal is *nix only, for Windows systems, you need to try something like this.

Community
  • 1
  • 1
NeoWang
  • 17,361
  • 24
  • 78
  • 126