I’m looking to call a python function from within another python script in a new thread.
I have experience using subprocess.Popen, but I used that for calling .exe’s in the command line. Anyone recommend how to do this or a module to use?
def main(argv):
call otherdef(1,2,3) in a new thread
sleep for 10 minutes
kill the otherdef process
def otherdef(num1, num2, num3):
while(True):
print num1
Here is a solution, but its not exactly like you asked, because its complicated to kill a thread. Its better to let the thread terminate itself, all threads default to daemonic=False (unless its parent thread is daemonic), so when the main thread dies, your thread would live. Set it to true, and it will die with your main thread.
Basically all you do is launch a
Threadand give it a method to run. You needed to be able to pass arguments so as you can see I pass anargs=parameter with the values to pass to the target method.Its still unclear if you want a process or a thread, but if you want a
Processimport multiprocessing and switchthreading.Thread(tomultiprocessing.Process(, everything else stays the same.