Use multiprocessing since you want to do multiple things at once https://pymotw.com/2/multiprocessing/basics.html If I understand you correctly, once the string is found you would terminate the process, so you would have to signal the calling portion of the code using a Manager dictionary or list.. I am not that knowledgeable about multiprocessing, so this is probably the long way around the barn. import time from multiprocessing import Process, Manager
def test_f(test_d): ctr=0 while True: ctr += 1 print "test_f", test_d["QUIT"], ctr time.sleep(1.0) if ctr > 5: test_d["QUIT"]=True if __name__ == '__main__': ## define the dictionary to be used to communicate manager = Manager() test_d = manager.dict() test_d["QUIT"] = False ## start the process p = Process(target=test_f, args=(test_d,)) p.start() ## check the dictionary every half-second while not test_d["QUIT"]: time.sleep(0.5) p.terminate() -- https://mail.python.org/mailman/listinfo/python-list