Use Threading. Thread module, there are two ways to use it, you can use a class, or you can pass in an instance of a function or class in an instantiated object.
#!/usr/bin/env python#-*-coding:utf-8-*-from Threading Import Threadimport timedef run_thread (n): for I in range (n ): Print I class race (Thread): def __init__ (self,threadname,interval): thread.__init__ (self,name= ThreadName) Self.interval = interval self.isrunning = True def run (self): while self.isrunning: print ' thread%s is running,time:%s\n '% (Self.getname (), Time.ctime ()) time.sleep (self.interval) def stop ( Self): self.isrunning = falsedef Test (): t1 = Thread (target=run_thread,args= (5,)) T1.start () Thread1 = Race (' A ', 1) thread2 = Race (' B ', 2) Thread1.start ( ) Thread2.start () time.sleep ( 4) thread1.stop () thread2.stop () if __name__ = = ' __main__ ': Test ()
Python threading Knowledge re-learning B