Python Forum

Full Version: Stop execution of a module
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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 !
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()
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.
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()
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 ?