I want to know how to stop a running function outside of it. Here is how it should be:
def smth(): time.sleep(5) # Just an example smth.stop() Thanks for your help
I want to know how to stop a running function outside of it. Here is how it should be:
def smth(): time.sleep(5) # Just an example smth.stop() Thanks for your help
Here's an example using the multiprocessing library:
from multiprocessing import Process import time def foo(): print('Starting...') time.sleep(5) print('Done') p = Process(target=foo) #make process p.start() #start function time.sleep(2) #wait 2 secs p.terminate() #kill it print('Killed') Output:
Starting... Killed Basically, what this code does is:
p which runs the function foo when startedp with p.terminate()Since p never passes time.sleep(5) in foo, it doesn't print 'Done'