[python] Problem s forkujicim serverem a SIGTERM signalem

Lumír Jasiok lumir.jasiok na vsb.cz
Středa Prosinec 17 16:23:28 CET 2008


Lumír Jasiok wrote:
> Leos Pol wrote:
>> To me privadi na myslenku, kdyz vidim ten try, jestli nechytas 
>> vyjimku SystemExit.
>>
>> David Michal wrote:
>>> Ja teda neumim vysvetlit proc nejde exit ale co to zkusit takhle:
>>>
>>> sysexit = False
>>>
>>> def cleanup(signal, frame):
>>>      """Clean up the server before shutdown.
>>>      """
>>>      # SIGTERM signal
>>>      if signal == 15:
>>>          print "Received SIGTERM signal, waiting for clients
>>>  termination"
>>>          while True:
>>>              # In case that there isn't any active clients
>>>              if not clients:
>>>                  break
>>>          sysexit = True
>>>
>>>  # Nadrazena smycka:
>>>   while not sysexit:
>>>          # Handle the child temination
>>>          signal.signal(signal.SIGCHLD,reap)
>>>  
>>>          # Catch the SIGTERM signal
>>>          signal.signal(signal.SIGTERM, cleanup)
>>>          #signal.pause()
>>>          try:
>>>              connection,address = sock.accept()
>>>
>>>  
>>>> -----Original Message-----
>>>> From: python-bounces na py.cz [mailto:python-bounces na py.cz] On Behalf Of
>>>> Lumír Jasiok
>>>> Sent: Wednesday, December 17, 2008 2:26 PM
>>>> To: Konference PyCZ
>>>> Subject: [python] Problem s forkujicim serverem a SIGTERM signalem
>>>>
>>>> Dobry den,
>>>>
>>>> potreboval bych poradit s nasledujicim problemem. Mam napsany
>>>> jednoduchy
>>>> forkujici TCP/IP server. Server jako takovy funguje, child procesy se
>>>> vytvareji i ukoncuji korektne, jediny probleme mam s ukoncenim parent
>>>> procesu v okamziku, kdy z (napriklad shellu) zavolam signal SIGTERM.
>>>> Mam
>>>> osetreno zachyceni signalu a funkci, ktera zajisti, ze se pocka na
>>>> ukonceni vsech child procesu a pak by se mel ukoncit i parent proces
>>>> pomoci sys.exit(0). To se ale nestane, parent proces (server) se
>>>> neukonci, ale skoci do nadrazene while smycky a dale ceka na spojeni a
>>>> je schopen obsluhovat prichozi spojeni. Relevantni kod:
>>>>
>>>>     
>>>
>>> _______________________________________________
>>> Python mailing list
>>> Python na py.cz
>>> http://www.py.cz/mailman/listinfo/python
>>>
>>>   
>>
> Ne, SystemExit nezachytavam, jeste jednou jsem zkontroloval kod, tim 
> to nebude. Jen ze zvedavosti zkusim prijit na to, co to zpusobuje. 
> Pokud na to prijdu, zkusim tady dat vedet, kdyby nekdo narazil na 
> stejny problem.
>
> Lumir
>
Takze nakonec to jde vyresit i pridanim zachytavani SystemExit a 
naslednym (brutalnim SIGKILL signalem) takto:

def cleanup(signal,frame):
    """Clean up the server before its shutdown.
    """
    # SIGTERM signal
    if signal == 15:
        print "Received SIGTERM signal, waiting for clients termination"
        while True:
            # In case that there isn't any live clients
            if not clients:
                break
        try:
            sys.exit(0)
        except SystemExit:
            os.kill(os.getpid(),9)
        except:
            traceback.print_exc

Neni to hezke, ale je to taky cesta. Co byste preferovali Vy? Prvni 
moznost pres promennou sysexit, nebo "hrubou silu"?

Lumir

-- 
 Lumír Jasiok
 VSB-TU Ostrava - Computer centre
 Tel: +420 59 732 3189
 E-mail: lumir.jasiok na vsb.cz
 http://www.vsb.cz
															



Další informace o konferenci Python