0

I use Python 2.7.3 on Windows.

Well. Now, think that there is a python program. For example let's name as "program.py" and it is content of program.py:

import program2
class program(object):
    def __init__(self):
        ...
        ... the jobs which program will do ...
        ...

    def function(self): #we call it from __init__ function when it's time came
        program2.start()

if __name__ == "__main__":
    p = program()

There is a file in same folder with program.py which named as program2.py. Let's give a content for program2.py:

def start():
    ....
    .... the jobs which program2.py will do ... # In this function we do jobs with while loops, threads etc. 
    ....
def stop():
    ...
    ... program2.py closes itself here ...
    ...

Think that I ran program.py . After a while I want to close program.py and I pressed Ctrl+C. But the threads in program2.py is running still. I want a thing like that program2.py will run stop() function and close itself. So on, the program will close with its all of the connections (like program2.py) . How can I do it ?

I searched signal module to do it but I'm not working on Linux. I'm working on Windows and I can't use a lot of feature of signal module. I succeeded it by creating a file which is named as 'stop.txt'. I mean that when the program.py take Ctrl+C command, it creates a file 'stop.txt' and program2.py controls if there is a file named as stop.txt in that folder. When program.py creates the file, program2.py sees it and closes itself. But this method is primitive, I think. Is there a more secret method ?

Thanks.

python_pardus
  • 310
  • 2
  • 5
  • 14

0 Answers0