Python Forum
random behavriour when handle process termination signal in Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
random behavriour when handle process termination signal in Python
#1
Hello,
I have a simple code, which has random behavior when termination signal is handled. I put the question in below link, with code, but no one replied yet.
I appreciate it, if anyone can take a look and help:

https://stackoverflow.com/questions/5724...-in-python
Reply
#2
Please do not link to another forum to ask a question.
Please show all details here.
Thank You
Reply
#3
(Jul-30-2019, 10:31 AM)Larz60+ Wrote: Please do not link to another forum to ask a question.
Please show all details here.
Thank You

Ok, this is full question:
I have a simple producer/consumer python code. consumer is in main process, but for producer a class is defined, where the class makes a process for producer.

by a counter limit, main process stops the job, and terminates the producer. If termination signal is not handled, everything finish fine. but, when I added a graceful termination flag, termination signal is captured, but in a random behavior, sometimes termination is not done correctly, and I get buffer full exception (while buffer size is larger than task size). Is there any idea why this simple code has an nondeterministic behavior?

import multiprocessing
import signal
from queue import Full

buf = multiprocessing.Queue(maxsize=100)

class GracefulKiller:
    kill_now = False
    #------------------------------
    def __init__(self):
        self.kill_now = False
        for signame in [signal.SIGTERM, signal.SIGQUIT, signal.SIGINT]:
            signal.signal(signame, self.exit_tasks)     

    #------------------------------     
    def exit_tasks(self, signum, frame):
        self.kill_now = True
        process_name = multiprocessing.current_process().name   
        print("Process {} captured termination signal:{}.".format(process_name, signum,)) 

class num_source():
    prc = None
    buf2 = None
    #------------------------------------------
    def __init__(self, buf):

        self.buf2 = buf
        self.prc = multiprocessing.Process( target=self.__producer_procces__, args=(self.buf2, ))
        self.prc.daemon = True
        self.prc.start()

    #------------------------------------------
    def read_num(self):
        return self.buf2.get(block=True, timeout=60) 

    #------------------------------------------
    def terminate_request(self):
        print("Termination of process requested.")
        self.prc.terminate()
        self.prc.join(10) 
        if self.prc.is_alive():
            print("process did NOT join")
        else:
            print("process joined")

    #------------------------------------------
    def __producer_procces__(self, buf3,):  
        killer = GracefulKiller()       
        while True:
            if killer.kill_now:
                break;
            num = 123 # or a random number          
            try:
                buf3.put(num, block=True, timeout=10)
            except Full:
                print("Buffer is full, but should not!")
        return 

if __name__ == '__main__':
    src_obj = num_source(buf)
    cnt = 0
    while True:
        cnt += 1
        num = src_obj.read_num()
        print("{}-{}".format(cnt, num))
        if (cnt == 50):
            break;
    src_obj.terminate_request()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Unexpected termination of program when using JDBC drivers in python with jaydebeapi skarface19 2 254 Feb-17-2024, 12:01 PM
Last Post: skarface19
  How do I handle escape character in parameter arguments in Python? JKR 6 1,039 Sep-12-2023, 03:00 AM
Last Post: Apoed2023
  Question about Creating an Automated Process in Python Supratik1234 0 712 Jan-13-2023, 08:29 PM
Last Post: Supratik1234
  Process finished with exit code 137 (interrupted by signal 9: SIGKILL) erdemath 2 9,388 Apr-18-2022, 08:40 PM
Last Post: erdemath
  Python multiprocessing Pool apply async wait for process to complete sunny9495 6 6,220 Apr-02-2022, 06:31 AM
Last Post: sunny9495
  Process the image on the Python HTTP server Aleks 0 3,153 Dec-02-2021, 11:43 PM
Last Post: Aleks
  CPC File Format (Cartesian Perceptual Compression) - Can Python Convert / Handle Them PSKrieger 2 2,418 Nov-11-2020, 02:57 PM
Last Post: PSKrieger
  How to to tie the execution of one process to another inside a loop in Python ignorant_wanderer 0 2,018 Jul-11-2020, 03:44 AM
Last Post: ignorant_wanderer
  win32 API: Launch Application to RDP session from background process python script rangeshgupta 0 2,092 May-28-2020, 09:41 PM
Last Post: rangeshgupta
  Spawning a new process that is not attached to python cman234 3 1,843 Apr-25-2020, 05:24 PM
Last Post: cman234

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020