Python Forum
Stop execution of a module - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Stop execution of a module (/thread-12754.html)



Stop execution of a module - catosp - Sep-11-2018

Hello everybody,

I have the following problem:
I have two scripts:
The main script:
import multiprocessing
import time
import module

def runModule():
    module


def main():
    p = multiprocessing.Process(target=runModule)
    p.start()
    time.sleep(10)
    p.terminate()


if __name__ == '__main__':
    main()
and the module script:
from time import sleep

for i in range(100):
    print i
    sleep(1)
The problem is the running module doesn't stop after 10s.
Is there any way to solve this?
Thank you !


RE: Stop execution of a module - Axel_Erfurt - Sep-11-2018

from time import sleep
import multiprocessing
import time
 
def runModule():
    module()
 
 
def main():
    p = multiprocessing.Process(target=runModule)
    p.start()
    time.sleep(10)
    p.terminate()

def module():
    for i in range(100):
        print (i)
        sleep(1)
 
 
if __name__ == '__main__':
    main()



RE: Stop execution of a module - catosp - Sep-11-2018

I don't want to include the code from the module inside the main file. What I want is to run the module file and to stop execution from the main thread.


RE: Stop execution of a module - Axel_Erfurt - Sep-11-2018

your file and method has same name(module)

try

from time import sleep
def run_module():
    for i in range(100):
        print (i)
        sleep(1)
from time import sleep
import multiprocessing
import time
import module
 
def runModule():
    module.run_module()
 
def main():
    p = multiprocessing.Process(target=runModule)
    p.start()
    time.sleep(10)
    p.terminate()
 
if __name__ == '__main__':
    main()



RE: Stop execution of a module - catosp - Sep-11-2018

Thank you very much! Can I apply this to my program without calling a specific function from the module? If I replace the module with a python program can I run it and stop it from my script ?